Run Node.js as a Service on Ubuntu


The core of our new project runs on Node.js. With Node you can write very fast JavaScript programs serverside. It’s pretty easy to install Node, code your program, and run it. But how do you make it run nicely in the background like a true server?

Clever chaps will have noticed you can just use the ‘&’ like so:

1
node ./yourprogram.js &

and send your program to the background. But:

  • if Node ever prints something and your console is closed, the STDOUT no longer exists and yourprogram.js will die
  • what if the process crashes, what if your server reboots?

Ok, so we needed something more robust. More like a real daemon, one that’s recognized by the Operating System as such.

Upstart

Our servers run Ubuntu’s latest: Karmic Koala, which packs a pretty decent version of upstart. Upstart will eventually replace the well-known /etc/init.d scripts, and will bring some additional advantages to the table like: speed, health checking, simplicity, etc.

Writing an upstart script

Turns out, writing your own upstart scripts is way easier than building init.d files based on the /etc/skeleton file.

Ok so here’s how it looks like; You should store the script in /etc/init/yourprogram.conf, create one for each Node program you write.

(nodejs.conf) download
1
2
3
4
5
6
7
8
9
10
11
12
description "node.js server"
author      "kvz"

start on startup
stop on shutdown

script
    # We found $HOME is needed. Without it, we ran into problems
    export HOME="/root"

    exec /usr/local/bin/node /where/yourprogram.js 2>&1 >> /var/log/node.log
end script

Dont forget to chmod u+x your file so it will be executable.

Wow how easy was that? Told you, upstart scripts are childsplay. In fact they’re so compact, you may find yourself changing almost every line cause they contain specifics to our environment.

non-root

Node can do a lot of stuff. Or break it if you’re not careful. So you may want to run it as a user with limited privileges. We decided to go conventional and chose www-data.

We found the easiest way was to prepend the Node executable with a sudo like this:

1
exec sudo -u www-data /usr/local/bin/node

Don’t forget to change your export HOME accordingly.

Restarting your Node.js daemon

This is so ridiculously easy..

1
2
start yourprogram
stop yourprogram

And yes, Node will already:

  • automatically start at boottime
  • log to /var/log/node.log

..that’s been defined inside our upstart script.

initctl

But wait, start and stop are just shortcuts. Who’s really behind the wheel here, is initctl. You can play around with the command to see what other possibilities there are:

1
2
3
4
5
initctl help
initctl status yourprogram
initctl reload yourprogram
initctl start yourprogram # yes, this is the same start
# etc

Update from October 30th, 2012

The basic idea has not changed since 2009, but we did add some tricks to our upstart script. Here’s what we now use in production at transloadit.com:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# cat /etc/init/transloaditapi2.conf 
# http://upstart.ubuntu.com/wiki/Stanzas

description "Transloadit.com node.js API 2"
author      "kvz"

stop on shutdown
respawn
respawn limit 20 5

# Max open files are @ 1024 by default. Bit few.
limit nofile 32768 32768

script
  set -e
  mkfifo /tmp/api2-log-fifo
  ( logger -t api2 </tmp/api2-log-fifo & )
  exec >/tmp/api2-log-fifo
  rm /tmp/api2-log-fifo
  exec sudo -u www-data MASTERKEY=`cat /transloadit/keys/masterkey` /transloadit/bin/server 2>&1
end script

post-start script
   /transloadit/bin/notify.sh 'API2 Just started'
end script

More on Node.js

With Node you can write very fast JavaScript programs serverside. We’ve seen examples of chat, key-value store, and full blown http servers. Basically anything is possible as long as you know JavaScript and the concepts of parallel/evented processing. You don’t? Well if you’ve ever used setTimeout(), you’ll soon get the hang of it 😉

Imported comments

These were imported from my old blog. Please use disqus below for new comments

Remco on 2012-06-22 15:44:16
Thanks for sharing your wisdom! I’ve used your example in combination with ‘forever’ to keep everything running smoothly, as explained on SO:

http://stackoverflow.com/questions/11084279/node-js-setup-for-easy-deployment-and-updating/11157223#11157223

kinzeron on 2012-06-11 20:12:29
Won’t be a problem to daemonize nodejs like this ? AFAIK a daemon needs two forks. Doing :

 nodejs ./path/code.js &
amp; 

would only fork it once if memory serves well. The daemon might still take terminal control! no ?

copy from http://kvz.io/blog/2009/12/15/run-nodejs-as-a-service-on-ubuntu-karmic/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s