Ubuntu 上适用于 nodejs + forever 的简单 upstart 脚本

Ubuntu 上适用于 nodejs + forever 的简单 upstart 脚本

我正在使用 Ubuntu Sever 12.04。我有一个 nodejs 应用程序,我想像这样运行:

NODE_ENV=production PORT=3001 APP_PATH=/var/www/myapp forever -a -l /var/www/myapp/forever.log -o /var/www/myapp/out.log -e /var/www/myapp/err.log /var/www/myapp/app.js

我想在系统启动时运行它,并在系统关闭时执行“forever stopall”。我读到,实现此目的的首选方法是使用 upstart 脚本。这就是我尝试的:

description "Upstart script"

start on startup
stop on shutdown

expect fork

env MYAPP_PATH="/var/www/myapp"

script

    NODE_ENV=production PORT=3001 APP_PATH=$MYAPP_PATH exec forever -a -l $MYAPP_PATH/forever.log -o $MYAPP_PATH/out.log -e $MYAPP_PATH/err.log  $MYAPP_PATH/app.js

end script

pre-stop script

    exec forever stop $MYAPP_PATH/app.js >> $LOG

end script

当我运行它时它会挂起,然后它说该作业已在运行(但实际上并未运行)。

任何关于如何正确执行此操作的建议都值得赞赏。谢谢!

答案1

description "Upstart script"

start on filesystem and started networking

供参考:不要使用“开机启动”

stop on shutdown

expect fork

# Let's make sure we always have the right directory
chdir /var/www/myapp

env MYAPP_PATH="/var/www/myapp"
env NODE_ENV=production
env PORT=3001
# Is there a reason you didn't do this?
env APP_PATH=$MYAPP_PATH

script
  # Shell trick to re-direct this script's STDOUT/STDERR
  exec 2>> forever.log 1>> forever.log
  # You forgot the 'start' command. Without it, it doesn't fork.
  exec forever start -a -l forever.log -o out.log -e err.log app.js
end script

pre-stop script
  # Shell trick to re-direct this script's STDOUT/STDERR
  exec 2>> forever.log 1>> forever.log
  exec forever stop app.js
end script

相关内容