Tim Smart describes in his article a great way to use upstart on Ubuntu to startup node.js applications.
Below is my version of his script. I stripped the PID file handling and moved configuration to use environment variables (to be able to use the same basescript for multiple apps). The start on -condition is modified according to comments posted on the article.
To use this you need to have upstart installed on your system. If you are running Ubuntu, you probably have. You put the following script to /etc/init/myapp.conf (obviously change the name to reflect your application). The name of the script also determines the log file that will be used under /var/log (UPSTART_JOB environment variable).
Once the script is in place, you case use the following commands to start/stop your application:
start myapp
stop myapp
status myapp
/etc/init/myapp.conf:
#!upstart description "node.js MyApplication" author "Juha Palomaki" env HOMEDIR=/var/myapp env RUNAS=myappuser env APP=/var/myapp/app.js env NODEJS=/usr/bin/node start on (local-filesystems and net-device-up IFACE=eth0) stop on shutdown script chdir $HOMEDIR exec sudo -u $RUNAS $NODEJS $APP >> /var/log/${UPSTART_JOB}.log 2>&1 end script pre-start script # Date format same as (new Date()).toISOString() for consistency echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/${UPSTART_JOB}.log end script pre-stop script echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/${UPSTART_JOB}.log end script
For more information on Upstart, check out the Upstart Cookbook.