Настройка пользователя в сценарии Upstart для Node.JS и Forever

962
sffc

Я запускаю веб-сайт, который использует Node.JS на сервере Ubuntu (14.04 LTS). После перезагрузки компьютера мой текущий рабочий процесс состоит в том, чтобы вручную запустить сервер вручную, выполнив команду

$ forever start app.js 

Сейчас я пытаюсь автоматизировать этот процесс с помощью Upstart. Я использовал chkconfig на RHEL раньше, но я новичок в Upstart. Я написал следующий скрипт и сохранил его в /etc/init/myapp.conf .

#!upstart  description "Start the node process with Upstart"   start on startup  stop on shutdown  expect fork   env APP_DIR="/path/to/app"  env NODE_BIN="/usr/local/bin"  env HOME_DIR="/home/admin"   script  date  echo "Starting"  su admin  cd $APP_DIR  PATH=$NODE_BIN:$PATH  HOME=$HOME_DIR  echo "Running forever start"  forever start app.js  end script   pre-stop script  date  echo "Stopping"  su admin  cd $APP_DIR  PATH=$NODE_BIN:$PATH  HOME=$HOME_DIR  echo "Running forever stop"  forever stop app.js  end script 

Тем не менее, сценарий, похоже, не работает. Когда я запускаю sudo start myappили sudo stop myappконсоль просто зависает.

Я бы хотел, чтобы Upstart как можно более близко имитировал то, что произойдет, если я просто войду на компьютер после перезагрузки и forever start app.jsзапуска, включая пользователя, выполняющего процесс, расположение файлов журнала по умолчанию и так далее.

Есть идеи?

0

1 ответ на вопрос

2
antino

by using upstart you'll be able stop using forever for your node process management. upstart has mechanisms for ensuring your process is started/stopped/restarted in the event of a failure (or nearly any other event for that matter)

here's a modified version of your upstart script that uses upstart's respawn directive to handle restarting your process if it crashes:

description "Start the node process with Upstart" author "you" # use if your process is going to fork *once* # expect fork # use if your process is going to fork *twice* # exepect daemon # respawn the process indefinitely # (ie. disregard the number of times it crashed # during a given interval and continuously # restart the process) respawn respawn limit unlimited setuid admin # ensures the process is only started once we have # filesystem access and an ethernet interface other # than loopback is available (ie. internet conn) # if your node process doesn't require net connectivity # just remove the `and net-device-up...` start on (local-filesystem and net-device-up IFACE!=lo) stop on shutdown # chdir sets the directory for all script blocks # (pre/post/errethang) chdir "/path/to/your/app" script # we're already in /path/to/your/app cause of `chdir` echo "app is starting" /usr/local/bin/node app.js 2>&1 > app.log end script pre-stop script echo "app is about to stop" end script 

finally, to be certain that there are no syntax errors in your service conf, you can use init-checkconf /etc/init/myapp.conf.

my main source of reference for upstart is here. this document is a bit heavyweight, though. and it's hard to discern real world usage from some of the examples.

Похожие вопросы