Config node.js as an upstart service

This How-To was wwritten for Ubuntu 12.10, though will likely also work for Ubuntu 12.04+ as well as other Linux flavours that support upstart Upstart.

1) Create an upstart script;

nano /etc/init/node.conf

2) add code like this:

#!upstart
description "node.js server"
author      "Ian Scrivener"

start on startup
stop on shutdown

# expect fork
# use daemon

script
    export HOME=        "/root"
    echo                 $$ > /var/run/node.pid
    cd                  /opt/promap/wwwroot
    exec                /usr/bin/node /opt/promap/wwwroot/app.js 2>&1 >> /var/log/node.log
end script


pre-start script
    # Date format same as (new Date()).toISOString() for consistency
    echo "[`date -u +%Y-%m-%dT%T.%3AU`] (sys) Starting" >> /var/log/node.log
end script

post-start script
   pgrep -f /usr/bin/node > /var/run/node.pid
end script

pre-stop script
    rm /var/run/node.pid
    echo "[`date -u +%Y-%m-%dT%T.%3AU`] (sys) Stopping" >> /var/log/node.log
end script

3) Create & chmod the log file

touch /var/log/node.log
chmod 666 /var/log/node.log

4) Start/Restart/Stop node.js

start node
restart node
stop node

5) View the upstart log

nano /var/log/upstart/node.log
# or
tail /var/log/upstart/node.log