kvz.io
Published on

Run Node.js as a Service on Ubuntu

Authors
  • avatar
    Name
    Kevin van Zonneveld
    Twitter
    @kvz

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

description "node.js server"
author      "kvz - https://kevin.vanzonneveld.net"

# Used to Be: Start on Startup
# until we found some mounts weren't ready yet while booting:
start on started mountall
stop on shutdown

# Automatically Respawn:
respawn
respawn limit 99 5

script
    # Not sure why $HOME is needed, but we found that it is:
    export HOME="/root"

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

post-start script
   # Optionally put a script here that will notifiy you node has (re)started
   # /root/bin/hoptoad.sh "node.js has started!"
end script

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

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

# cat /etc/init/transloaditapi2.conf
# https://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 ; )

Legacy Comments (38)

These comments were imported from the previous blog system (Disqus).

Felix Geisendörfer
Felix Geisendörfer·

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

Kev van Zonneveld
Kev van Zonneveld·

@ 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!

Mariano Iglesias
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 &

Kev van Zonneveld
Kev van Zonneveld·

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

sveisvei
sveisvei·

Useful stuff, thanx :)

jz
jz·

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

Kev van Zonneveld
Kev van Zonneveld·

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

George
George·

Thanks 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!

Kev van Zonneveld
Kev van Zonneveld·

@ George: Cool thanks for sharing!

Francisco
Francisco·

Thanks for the write up!

Hey guys did you manage to run a real-world program? Mine requires a library with a relative path so I can\'t call it from anywhere (like, node /path/to/my.js).

I either need to cd /path/to && node my.js , or use chdir - but none has worked for me. Any ideas?

Francisco
Francisco·

I was being a bit silly, I fixed my program to be executable from an absolute path.

I copy-pasted the example, only replacing /where/yourprogram.js for my program - but after \"sudo start myprogram\" it must crash. Nothing is logged. Using upstart 0.6.3-11 on karmic.

Kev van Zonneveld
Kev van Zonneveld·

@ Francisco: Did you get it working? As for the real-world program, here it is: http://transloadit.com/

AFire
AFire·

[CODE=\"text\"] node ./yourprogram.js | tee -a node.log & [/CODE]

Xavier
Xavier·

For whatever reasons I cannot make it work. My program doesn\'t seem to run in the same environment and path.exists returns errors when node is launched via initctl.
I checked the require.paths array and it is exactly the same whether I run directly node app.js or if I do \"start node\".

I also tried to to run \"env\" before (sudo -u myuser env /usr/local/bin/node /path/app.js ...) but doesn\'t help.

I run out of ideas. Thanks for your support.

Kev van Zonneveld
Kev van Zonneveld·

@ AFire: Read more carefully. The first message your script sends to STDERR, and your terminal is closed, your script is killed. But you\'ve probably figured that out, or resorted to using monit by now.

@ Xavier: The same way that I:
export HOME=\"/root\"

You could export your PATH as well if that\'s required.
so first echo $PATH, and put that in there

Sami Samhuri
Sami Samhuri·

Thanks Kevin! This is exactly what I needed.

watch hellcats online
watch hellcats online·

copy-pasted the example, only replacing /where/yourprogram.js for my program - but after "sudo start myprogram" it must crash. Nothing is logged. Using upstart 0.6.3-11 on karmic.

Kev van Zonneveld
Kev van Zonneveld·

@ hellcats: well try debugging it a little.
what happens when you just run

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

what about:

/usr/local/bin/node /where/yourprogram.js ?

Ovidiu
Ovidiu·

Awesome tutorial, thank you, works really well!

peter host
peter host·

Clear and to the point :)
As a complement for users who like it the init.d way :
https://gist.github.com/715255

ipage
ipage·

This is what I have been looking for! Thanks for this helpful tutorial!

Kev van Zonneveld
Kev van Zonneveld·

@ peter host: Thanks for sharing. Keep in mind upstart will replace init.d in the long run though.

Eran Hammer-Lahav
Eran Hammer-Lahav·

Should it be:

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

instead? The original version does not save exceptions to the log file.

Joey
Joey·

Thanks for the init.d version, Peter!

For anyone (like me) running a server on an older distro release, using Upstart is either not possible, impractical, or overkill when you already have init.d.

And for anyone (again, like me) running a server on a new distro, you're at least as likely to have systemd (which will replace Upstart in the long run) available to you as you are Upstart.

I think I'll stick with good ol' init.d (which is available & highly compatible on every distro I've installed) until the distros figure this out ;)

Kev van Zonneveld
Kev van Zonneveld·

@ Eran Hammer-Lahav: Thanks, changed it.

@ Joey: Sure : ) I just like how small upstart scripts can be and that it can resuscitate crashed programs.

aaronfay
aaronfay·

Simply awesome, thanks for the upstart info. Being able to 'restart node-app' is just bloody awesome.

Thanks,
Aaron

Geoff Wagstaff
Geoff Wagstaff·

Great write-up on getting upstart working. For those who want to have node processes persist after they close the terminal without upstart, you could use nohup:

[CODE="text"]nohup node script.js > /dev/null[/CODE]

dale
dale·

i can't seem to get this to work correctly. I have always type in `sudo start myapp` in order for this to start. So when i reboot my server, this script does not fire at all. I actually changed the user:group to my www-data:www-data user/group, and gave the file permissions of 774. but even with that, i still need to run sudo to get the file to start. any clues on what else i may be able to do to get this to start on server reboot?

Herman A. Junge
Herman A. Junge·

Thanks a Lot. I was looking for an elegant solution to that problem.

hermanjunge

Mauvis Ledford
Mauvis Ledford·

Just a note that I followed your directions on Ubuntu 11.04 and everything worked except for on reboot.

With some research I ended up modifying this line to get it working:

from: start on started mountall
tp: start on (local-filesystems and net-device-up IFACE=eth0)

I guess, the internet wasn't ready when the script was executing.

Cheers,

Mauvis

claude hussenet
claude hussenet·

Works like a charm !

Claude Hussenet

Rindra
Rindra·

Hi,

First i would like to say that this works really great as is. Thanks a lot!
Now I've tried to use the upstart script with a static file server I've created with node.js
When I run it from the terminal: node main.js everything works fine in the browser. The issue occurs when i start my server as a daemon, start app, nothing appears in the browser.

I'm starting on node.js and i love the platform, so is this a misconception on my end? I would love to get your input on this.

Thanks again!

Andy
Andy·

Do you think this could work on CentOs?

h
h·

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

mark
mark·

100% perfect!

Arcadius
Arcadius·

Hi.
I have seen your presentation nodejs-in-production on slideshare.
My question is: Why do you serve only a part of the site on node.js? I would have thought that node.js could handle it all and that the ngix/php bit would go away.

Thanks.

Arcadius.

kinzeron
kinzeron·

Won't be a problem to daemonize nodejs like this ? AFAIK a daemon needs two forks. Doing : [CODE="text"] nodejs ./path/code.js & [/CODE]
would only fork it once if memory serves well. The daemon might still take terminal control! no ?

Remco
Remco·

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