系统重启时自动永久永久启动(节点)

Node.js

猪猪

2020-03-23

我正在使用节点的永久模块来保持节点服务器运行。但是,当系统重新启动时,永久终止。系统重启时,有什么方法可以自动(永久)自动启动节点服务器?

第3088篇《系统重启时自动永久永久启动(节点)》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

14个回答
Jim小胖米亚 2020.03.23

您可以在外壳程序中使用以下命令永久启动节点:

forever app.js //my node script

您需要记住,运行应用程序的服务器应始终保持打开状态。

番长樱梅 2020.03.23

完整的示例crontab(位于/ etc / crontab中)。

#!/bin/bash

# edit this file with .. crontab -u root -e
# view this file with .. crontab -u root -l

# put your path here if it differs
PATH=/root/bin:/root/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin

# * * * * * echo "executes once every minute" > /root/deleteme

@reboot cd /root/bible-api-dbt-server; npm run forever;
@reboot cd /root/database-api-server; npm run forever;
@reboot cd /root/mailer-api-server; npm run forever;
伽罗 2020.03.23

rc.local的问题在于命令是以root身份访问的,这与以用户身份登录和使用sudo的身份不同。

我通过向我的etc / profile.d添加启动命令添加.sh脚本来解决此问题。profile.d中的任何.sh文件都将自动加载,并且任何命令都将被视为您使用常规sudo。

唯一的缺点是指定的用户需要登录才能开始事情,在我看来,这种情况总是如此。

2020.03.23

我尝试了很多上述答案。他们都没有为我工作。我的应用程序是/home作为用户安装的,而不是root用户。这可能意味着当上述启动脚本运行时,/home尚未安装,因此该应用程序尚未启动。

然后我通过Digital Ocean找到了这些说明:

https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps

如前所述,使用PM2非常简单并且可以完美运行:自从我的虚拟服务器发生了两次物理崩溃-停机时间只有一分钟左右。

斯丁 2020.03.23

我编写了一个脚本来执行此操作:

https://github.com/chovy/node-startup

我还没有尝试过,但是您可以自定义它运行的命令,因此应该很简单:

/etc/init.d/node-app start
/etc/init.d/node-app restart
/etc/init.d/node-app stop
村村 2020.03.23

使用PM2

哪个是运行服务器生产服务器的最佳选择

以这种方式运行应用程序有哪些优势?

  • 如果PM2崩溃,它将自动重新启动您的应用程序。

  • PM2将记录您未处理的异常的日志-在这种情况下,将记录在/home/safeuser/.pm2/logs/app-err.log中的文件中。

  • 使用一个命令,PM2可以确保在服务器重新启动时,它管理的所有应用程序都重新启动。基本上,您的节点应用程序将作为服务启动。

参考:https : //www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps

Mandy猴子阿飞 2020.03.23

永远无法使节点应用程序作为服务运行。正确的方法是创建/ etc / inittab条目(旧的linux系统)或新贵(新的linux系统)。

这是一些有关如何将其设置为暴发户的文档:https : //github.com/cvee/node-upstart

小宇宙 2020.03.23

crontab在CentOS x86 6.5上对我不起作用。@reboot似乎无法正常工作。

终于我得到了这个解决方案:

编辑: /etc/rc.local

sudo vi /etc/rc.local

将此行添加到文件末尾。改变USER_NAMEPATH_TO_PROJECT你自己。NODE_ENV=production表示该应用在生产模式下运行。如果您需要运行多个node.js应用程序,则可以添加更多行。

su - USER_NAME -c "NODE_ENV=production /usr/local/bin/forever start /PATH_TO_PROJECT/app.js"

不要NODE_ENV单独设置行,您的应用仍将在开发模式下运行,因为永远不会获得NODE_ENV

# WRONG!
su - USER_NAME -c "export NODE_ENV=production"

保存并退出vi(按ESC : w q return)。您可以尝试重新启动服务器。服务器重启后,即使您没有通过ssh远程登录任何帐户,node.js应用程序也应自动运行。

您最好NODE_ENV在shell中设置环境。NODE_ENV将在您的帐户USER_NAME登录时自动设置

echo export NODE_ENV=production >> ~/.bash_profile

因此,您可以/PATH_TO_PROJECT/app.js通过ssh 运行永久停止/启动之类的命令,而无需NODE_ENV再次设置

小宇宙 2020.03.23

您需要为此在/etc/init.d文件夹中创建一个shell脚本。如果您从未做过,那会很复杂,但是在Web上有大量关于init.d脚本的信息。

这是一个示例脚本,我创建了该脚本以永久运行CoffeeScript网站:

#!/bin/bash
#
# initd-example      Node init.d 
#
# chkconfig: 345 
# description: Script to start a coffee script application through forever
# processname: forever/coffeescript/node
# pidfile: /var/run/forever-initd-hectorcorrea.pid 
# logfile: /var/run/forever-initd-hectorcorrea.log
#
# Based on a script posted by https://gist.github.com/jinze at https://gist.github.com/3748766
#


# Source function library.
. /lib/lsb/init-functions


pidFile=/var/run/forever-initd-hectorcorrea.pid 
logFile=/var/run/forever-initd-hectorcorrea.log 

sourceDir=/home/hectorlinux/website
coffeeFile=app.coffee
scriptId=$sourceDir/$coffeeFile


start() {
    echo "Starting $scriptId"

    # This is found in the library referenced at the top of the script
    start_daemon

    # Start our CoffeeScript app through forever
    # Notice that we change the PATH because on reboot
    # the PATH does not include the path to node.
    # Launching forever or coffee with a full path
    # does not work unless we set the PATH.
    cd $sourceDir
    PATH=/usr/local/bin:$PATH
    NODE_ENV=production PORT=80 forever start --pidFile $pidFile -l $logFile -a -d --sourceDir $sourceDir/ -c coffee $coffeeFile

    RETVAL=$?
}

restart() {
    echo -n "Restarting $scriptId"
    /usr/local/bin/forever restart $scriptId
    RETVAL=$?
}

stop() {
    echo -n "Shutting down $scriptId"
    /usr/local/bin/forever stop $scriptId
    RETVAL=$?
}

status() {
    echo -n "Status $scriptId"
    /usr/local/bin/forever list
    RETVAL=$?
}


case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        restart
        ;;
    *)
        echo "Usage:  {start|stop|status|restart}"
        exit 1
        ;;
esac
exit $RETVAL

由于init.d脚本是以root身份运行的,因此我必须确保文件夹和PATH是显式设置的或对root用户可用。

小胖Gil 2020.03.23
  1. 使用NPM全局安装PM2

    npm install pm2 -g

  2. 用pm2启动脚本

    pm2 start app.js

  3. 生成活动的启动脚本

    pm2 startup

    注:pm2启动是用于在系统重新引导时启动PM2。PM2一旦启动,将重新启动系统停机之前一直在管理的所有进程。

如果要禁用自动启动,只需使用pm2 unstartup

如果您要在其他用户下执行启动脚本,只需使用-u <username>选项和--hp <user_home>:

小胖 2020.03.23

另一种方法crontab的灵感来自这个答案,这个博客帖子。

1.创建一个bash脚本文件(将bob更改为所需的用户)。

vi /home/bob/node_server_init.sh

2.将其复制并粘贴到刚创建的文件中。

#!/bin/sh

export NODE_ENV=production
export PATH=/usr/local/bin:$PATH
forever start /node/server/path/server.js > /dev/null

确保根据您的配置编辑以上路径!

3.确保可以执行bash脚本。

chmod 700 /home/bob/node_server_init.sh

4.测试bash脚本。

sh /home/bob/node_server_init.sh

5.将“ bob”替换为节点的运行时用户。

crontab -u bob -e

6.复制并粘贴(将鲍勃更改为所需的用户)。

@reboot /bin/sh /home/bob/node_server_init.sh

保存crontab。

您已经做到最后,您的奖金是重新启动(测试):)

Mandy 2020.03.23

我建议使用crontab。易于使用。

如何

  1. 要开始编辑,请运行以下操作,将“ testuser”替换为所需的节点进程运行时用户。如果您选择自己以外的其他用户,则必须使用sudo运行它。

    $ crontab -u testuser -e
    
  2. 如果您以前从未做过此操作,它将询问您希望使用哪个编辑器。我喜欢vim,但会推荐使用nano以便于使用。

  3. 进入编辑器后,添加以下行:

    @reboot /usr/local/bin/forever start /your/path/to/your/app.js
    
  4. 保存文件。您应该得到一些有关cron已安装的反馈。

  5. 为了进一步确认cron的安装,请执行以下操作(再次用目标用户名替换“ testuser”)以列出当前安装的cron:

    $ crontab -u testuser -l 
    

请注意,我认为在cron中执行二进制文件时应始终使用完整路径。另外,如果永久脚本的路径不正确,请运行which forever以获取完整路径。

Given that forever calls node, you may also want to provide the full path to node:

@reboot /usr/local/bin/forever start -c /usr/local/bin/node /your/path/to/your/app.js

Further Reading

DavaidTony宝儿 2020.03.23

这种情况对Debian有效。

将以下内容添加到 /etc/rc.local

/usr/bin/sudo -u {{user}} /usr/local/bin/forever start {{app path}}

  • {{user}} 替换您的用户名。
  • {{app path}}替换您的应用路径。例如,/var/www/test/app.js
神乐 2020.03.23

您可以使用永久服务来做到这一点。

npm install -g forever-service
forever-service install test

这将通过永久方式将当前目录中的app.js作为服务提供。每当系统重新启动时,该服务将自动重新启动。同样,当停止时,它将尝试平稳停止。该脚本还提供了logrotate脚本。

Github网址:https : //github.com/zapty/forever-service

注意:我是永远服务的作者。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android