我正在使用node的forever模块来保持我的节点服务器运行。但是,当系统重新启动时,永久终止。有什么方法可以在系统重新启动时自动启动节点服务器(永久启动)?
我建议使用crontab。 它易于使用。
如何
-
要开始编辑,请运行以下命令,将"testuser"替换为节点进程所需的运行时用户。 如果您选择除自己以外的其他用户,则必须使用 sudo 运行此用户。
$ crontab -u testuser -e
-
如果您以前从未这样做过,它会询问您希望使用哪个编辑器进行编辑。 我喜欢vim,但会推荐nano易于使用。
-
进入编辑器后,添加以下行:
@reboot /usr/local/bin/forever start /your/path/to/your/app.js
-
保存文件。 您应该得到一些反馈,表明 cron 已安装。
-
要进一步确认 cron 的安装,请执行以下命令(再次将"testuser"替换为您的目标用户名)以列出当前安装的 crons:
$ crontab -u testuser -l
请注意,在我看来,在 cron 中执行二进制文件时应始终使用完整路径。 此外,如果永久脚本的路径不正确,请运行which forever
以获取完整路径。
鉴于forever
调用node
,您可能还需要提供node
的完整路径:
@reboot /usr/local/bin/forever start -c /usr/local/bin/node /your/path/to/your/app.js
延伸阅读
- crontab 手册页
- Ubuntu Cron HowTo
您可以使用永久服务来执行此操作。
npm install -g forever-service
forever-service install test
这将通过永久将当前目录中的 app.js 作为服务进行配置。每次重新启动系统时,该服务都会自动重新启动。同样,当停止时,它将尝试优雅地停止。此脚本也预配日志旋转脚本。
Github网址:https://github.com/zapty/forever-service
注意:我是永远服务的作者。
-
使用 NPM 全局安装 PM2
npm install pm2 -g
-
使用 pm2 启动脚本
pm2 start app.js
-
生成活动启动脚本
pm2 startup
注意:pm2 启动用于在系统重新启动时启动 PM2。PM2 一旦启动,就会重新启动它在系统出现故障之前管理的所有进程。
如果您想禁用自动启动,只需使用 pm2 取消启动
如果您希望在其他用户下执行启动脚本,只需使用-u <username>
选项和--hp <user_home>:
这种情况对 Debian 有效。
将以下内容添加到/etc/rc.local
/usr/bin/sudo -u {{user}} /usr/local/bin/forever start {{app path}}
{{user}}
替换您的用户名。{{app path}}
替换应用路径。例如,/var/www/test/app.js
受此答案和这篇博客文章启发的另一种 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. 复制并粘贴(将 bob 更改为所需用户)。
@reboot /bin/sh /home/bob/node_server_init.sh
保存 crontab。
你已经走到了最后,你的奖品是重新启动(测试):)
从附加的问题中复制答案。
您可以使用 PM2,它是具有内置负载均衡器的 Node.js 应用程序的生产过程管理器。
安装 PM2
$ npm install pm2 -g
启动应用程序
$ pm2 start app.js
如果您使用快速,那么您可以像
pm2 start ./bin/www --name="app"
列出所有正在运行的进程:
$ pm2 list
它将列出所有进程。然后,可以通过以下命令使用应用的 ID 或名称来停止/重新启动服务。
$ pm2 stop all
$ pm2 stop 0
$ pm2 restart all
显示日志
$ pm2 logs ['all'|app_name|app_id]
你需要在/etc/init.d 文件夹中创建一个 shell 脚本。如果您从未做过,这有点复杂,但是 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 身份运行。
使用 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
Forever 不是为了让节点应用程序作为服务运行。正确的方法是创建一个/etc/inittab 条目(旧的 Linux 系统)或一个新贵(较新的 Linux 系统)。
以下是有关如何将其设置为暴发户的一些文档: https://github.com/cvee/node-upstart
crontab
在 CentOS x86 6.5 上对我不起作用。 @reboot似乎不起作用。
最后我得到了这个解决方案:
编辑:/etc/rc.local
sudo vi /etc/rc.local
将此行添加到文件末尾。将USER_NAME
和PATH_TO_PROJECT
更改为您自己的。NODE_ENV=production
表示应用在生产模式下运行。如果需要运行多个节点.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 远程登录任何帐户,您的节点.js应用程序也应自动运行。
你最好在你的外壳中设置NODE_ENV
环境。NODE_ENV
将在您的帐户USER_NAME
登录时自动设置。
echo export NODE_ENV=production >> ~/.bash_profile
因此,您可以通过 ssh 运行永久停止/启动/PATH_TO_PROJECT/app.js
等命令,而无需再次设置NODE_ENV
。
我写了一个脚本来做这个事情:
https://github.com/chovy/node-startup
我没有尝试过,但是您可以自定义它运行的命令,因此它应该很简单:
/etc/init.d/node-app start
/etc/init.d/node-app restart
/etc/init.d/node-app stop
rc.local 的问题在于命令以 root 身份访问,这与以用户身份登录并使用 sudo 不同。
我通过添加一个带有我想要的启动命令的.sh脚本来解决这个问题 etc/profile.d.profile.d 中的任何.sh文件都将自动加载,任何命令都将被视为使用常规 sudo。
唯一的缺点是指定的用户需要登录才能开始工作,而在我的情况下总是如此。
我尝试了很多上述答案。他们都不适合我。我的应用程序以用户身份安装在/home
中,而不是以 root 身份安装。这可能意味着当上面提到的启动脚本运行时,/home
尚未挂载,因此应用程序尚未启动。
然后我通过数字海洋找到了这些说明:
https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps
如前所述使用PM2非常简单且运行良好:我的虚拟服务器发生了两次物理崩溃 - 停机时间只有大约一分钟。
完整的示例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;
我通过使用serve和npm找到了自己的解决方案,如下所示:
- 安装服务包:
npm install -g serve
- 然后
serve -s /var/www/sitename
命令在重新启动时执行。
这就是在我的VPS上对我有用的。
您可以在 shell 中使用以下命令永久启动节点:
forever app.js //my node script
您需要记住,运行应用的服务器应始终保持打开状态。