» Run Node.js as a Service on Ubuntu Karmic

On twitter

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:

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 features 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.

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:

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.. 

start yourprogram
stop yourprogram

And yes, Node will already:

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

..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:

initctl help
initctl status yourprogram
initctl reload yourprogram
initctl start yourprogram # yes, this is the same start

etc

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 ; ) 

  1. Node.js video presentation by creator Ryan Dahl
  2. Node.js slides that accompany the presentation
  3. About Node on the official website
  4. Node.js is genuinely exciting by Simon Willison
  5. node.js by Debuggable

Stay up to date

You can track my blog rss articles and rss comments. You may also find my rss bookmarks interesting. Or twitter Follow me on Twitter


Like this article?

   Then Digg it!
Or use another bookmark button below to show your support &
help me spread the word.


tags: nodejs, ubuntu, karmic, upstart, daemon, transloadit, php
category: Programming - Javascript - Node.js
read: 4,409 times

Add comment

(required, shown)(required, not shown)for syntax highlighting

[CODE="Javascript"]
your_code_here();
[/CODE]

Replace "Javascript"
with "php", "text", etc.
code (to make sure you are not a spammer)

 Track replies: rss feed comments feed

Comments

#9. Kevin on 26 February 2010

Twitter.com: kvz@ George: Cool thanks for sharing!

#8. George on 25 February 2010

Gravatar.com: GeorgeThanks for this, worked like a charm.

The only thing I had to change was that I change to the directory containing my .js file before launching node. For whatever reason, I otherwise have trouble loading static files into my node.js app.

Cheers!

#7. Kevin on 07 January 2010

Twitter.com: kvz@ jz: Correct, I actually had to change it from /etc/event.d to /etc/init/ when we upgraded to Karmic. It looked as if they were going to stick with /etc/init in the long run though, so be prepared to change it back at one point ;)

#6. jz on 25 December 2009

Gravatar.com: jzthanks for the writeup!

for some reason, on 8.10 i had to put the conf file in /etc/event.d for initctl to find it...

#5. sveisvei on 16 December 2009

Gravatar.com: sveisveiUseful stuff, thanx :)

#4. Kevin on 15 December 2009

Twitter.com: kvz@ Mariano Iglesias: Hey. That's a trick I didn't know yet, thanks a lot Mariano!

#3. Mariano Iglesias on 15 December 2009

Gravatar.com: Mariano Iglesias@Felix: also, if you want to run a script in background, and not link it to your session (so if you logout it doesnt die), use nohup:

$ nohup script &

#2. Kevin on 15 December 2009

Twitter.com: kvz@ Felix Geisendörfer: Thanks for adding some sugar to the post! While screen won't give you healthchecks, or come online when your server recovers from a crash, it's definitely another great tool in our arsenal!

#1. Felix Geisendörfer on 15 December 2009

Gravatar.com: Felix GeisendörferCool article Kevin, glad you published this : )!

If you just need something slightly better than "node ./yourprogram.js &", because you might just be doing a long running job, you can type in "screen" and then execute "node ./yourprogram.js" in the new terminal that opens. Screen sessions don't die if you log out, and you can even re-attach them using "screen -r" when you login the next time.