1

I deployed my node.js app to Digital Ocean, using dokku (Docker powered mini-Heroku). The app is started by command in Procfile (web: node app.js).
How do I start it with Upstart, so that it restarts automatically after crashing?

Added:
I need it to be upstarted when I deploy with git push dokku master.

4

2 回答 2

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

于 2014-11-07T09:39:05.123 回答
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
于 2018-12-12T22:25:54.200 回答