5n9bgg
Last Updated: February 25, 2016
·
4.116K
· subicura

Run Node.js as a Service on Ubuntu

  • use upstart
sudo apt-get install upstart
  • write script /etc/init/your_program.conf

example

#!upstart
description "node.js chatapp server"
author      "subicura"

start on startup
stop on shutdown

script
    export HOME="/home/hosting"

    echo $$ > /var/run/chatapp.pid
    exec sudo -u hosting /usr/local/bin/node /hosting/nemoz/current/lib/nodejs/chatapp.js >> /var/log/chatapp.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/chatapp.log
end script

pre-stop script
    rm /var/run/chatapp.pid
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/chatapp.log
end script
  • start/stop
/sbin/start your_program
/sbin/stop your_program