Adding your Node.JS app as a service in Ubuntu

A Blog Post by

Adding a Node.js application as a service on your Ubuntu server is pretty easy using Ubuntu's Upstart. Just create a config file at /etc/init/myapp.conf with the following:

#/etc/init/myapp.conf
description "Your app name here"
author "This would be you"

# Start the service after everything loaded
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown

# Automatically restart service
respawn
respawn limit 99 5

script
    # Navigate to your app directory
    cd /path/to/myapp/
   
    # Run the script with Node.js and output to a log
    exec /path/to/node_install app.js 2>&1 >> /var/log/myapp.log
end script

Now you can start/stop your app as a service:

sudo start myapp
sudo restart myapp
sudo stop myapp

It's handy to keep an eye on your script too:

tail /var/log/myapp.log -f

For more information on keeping your script alive, you can have a look at at Jacob Mumm's blog post.

Created: Mon, 20/08/2012 - 18:15