永远运行Upstart

时间:2014-04-07 21:04:05

标签: node.js init upstart forever

我们在Ubuntu(13.10)服务器上有一系列node.js脚本,我们希望尽可能地保持运行,并在服务器重启时自动重启。我们尝试了一些不同的技术,但尚未找到有效的解决方案。

设置:没有脚本在端口80上运行,而是在8000以上的端口上运行。

Node.js个文件目前正在/usr/lib/sites/path/Node

中运行

我们已经使用Forever单独设置并运行,同时在具有良好特权(但不是root用户)的用户的上下文中运行,只需在包含脚本的文件夹的上下文中运行以下内容:

forever start server_process.js

我们希望在服务器启动时运行这些脚本,并且稍后(如果需要)有一些能力来重新启动它们。

Upstart听起来应该是解决方案,但我们还没有设法让它发挥作用。以下脚本启动,然后停止而不指示原因...

description "Our app"
version "1.0"
author "Nautilytics"

start on startup
stop on shutdown

expect fork

env FOREVER_PATH=/usr/bin/forever
env APPLICATION_DIRECTORY=/usr/lib/sites/path/Node
env APPLICATION_START=ourapp.js
env LOG_PATH=/var/log/ourapp.log

chdir /usr/lib/sites/path/Node

script
    exec $FOREVER_PATH start --sourceDir $APPLICATION_DIRECTORY -f -v $APPLICATION_START >> $LOG_PATH 2&>1
end script

通过直接的反复试验,我们曾多次遇到错误,指出无法找到其他文件(ourapp.js所需),就像chdir无法正常工作一样或者通过永远的开始。

3 个答案:

答案 0 :(得分:4)

在搜索互联网寻求解决方案之后,我们决定坚持使用nodejs而不是永远用于此任务。它更容易,并且通过重生它可以完成我们需要永远为我们做的一切。

start on runlevel [2345]

stop on shutdown

respawn

script
    exec sudo nodejs /usr/lib/sites/path/Node/ourapp.js 2>&1 >> /var/log/ourapp.log
end script

答案 1 :(得分:1)

更安全的upstart配置脚本可能是(hardened-nodejs.conf):

description "Managing and monitoring nodejs application"

# start when filesystem is mounted networking is up
start on (filesystem and net-device-up IFACE!=lo)
# stop on shutting down the system
stop on runlevel [016]

# application environment
# staging and development instances should use hardened-nodejs.override to define environment
env NODE_ENV=production

# respawn the job up to 10 times within a 5 second period.
# If the job exceeds these values, it will be stopped and marked as failed.
respawn
respawn limit 10 5

# ssl-cert group can read certificates
setuid www-data
setgid ssl-cert

exec  /usr/bin/nodejs /usr/lib/sites/path/Node/ourapp.js 2>&1 >> /var/log/ourapp.log

在低端口上运行nodejs应用程序监听的解决方案之一可能是使用capabilities tools

如果通过SSL用户或组提供服务,则必须具有对证书的读取权限。查看ssl-cert packagethis answer了解基本概念。

答案 2 :(得分:0)

你检查过process.cwd()吗?可能是您的流程正在其他地方运行。

相关问题