我使用dokku(Docker驱动的mini-Heroku)将我的node.js应用程序部署到Digital Ocean。该应用程序由Procfile(web: node app.js
)中的命令启动
如何使用Upstart启动它,以便在崩溃后自动重启?
添加了:
当我使用git push dokku master
进行部署时,我需要它是新手。
答案 0 :(得分:1)
编辑文件/etc/init/node.conf
并输入以下代码。将/opt/node_project/
更改为项目的路径。编辑此文件时,您需要是root用户,因此请使用sudo
打开您的编辑器。
description "Node server"
author "eagor"
# Stanzas
#
# Stanzas control when and how a process is started and stopped
# See a list of stanzas here: http://upstart.ubuntu.com/wiki/Stanzas#respawn
# When to start the service
start on runlevel [2345]
# When to stop the service
stop on runlevel [016]
# Automatically restart process if crashed
respawn
script
echo $$ > /var/run/node.pid;
exec node /opt/node_project/app.js
end script
post-stop script
rm -f /var/run/node.pid
end script
现在您已为流程创建了Upstart配置,您可以从命令行启动它:
$ sudo service node start
Upstart将监控您的流程,并在任何时候停止运行。
它还会将日志重定向到/var/log/upstart/node.log
。
以上与节点直接相关并绕过Dokku。看起来像Upstart并不是解决这个问题的最好方法。
您应该考虑使用forever
模块。将forever
添加到package.json依赖项中。然后在您的Procfile中使用:web: ./node_modules/forever/bin/forever app.js
答案 1 :(得分:0)
从Dokku 0.7.0开始,它内置了重新启动策略:
https://github.com/dokku/dokku/blob/master/docs/deployment/process-management.md#restart-policies
例如
# only restart it on Docker restart if it was not manually stopped
dokku ps:set-restart-policy node-js-app unless-stopped