pieter michels

Github    Ask me anything   

March 25, 2011 at 7:06pm

Home

Node.js with Upstart and Monit

I’ve been using Forever for a while now to keep my Node.js applications up and running on my Linode, Ubuntu, VPS. This works pretty good, as you can easily start, stop and list all your apps running with Forever. Basically, it allows you to start a node process in the background so you don’t block your current shell and it restarts your app when it suddenly decides to die on you. What else do you need?

Great. But I was wondering if managing your apps could be ‘easier’ and more centralized, and with more options if needed.

Upstart + Monit.

A lot of walk-throughs have been written so far on Upstart and Monit. What I like about Upstart is the fact that it is more generic than Forever: it’s not solely for keeping a Node.js service alive or daemonized: it can be used for whatever service you want to have up and running when your system restarts for example.

Upstart is installed by default on Ubuntu but compiling it yourself shouldn’t be that difficult. It is said to replace the /etc/init.d/* service scripts in favor of writing job definition for Upstart.

An Upstart job.

Our Upstart job scripts will be very straightforward.

We want Upstart to start, monitor and restart our application. I told Upstart to restart (respawn) my app when it dies and stop doing this when the respawning fails, or was repeated, 5 times in 60 seconds.

I use this for one of my Node.js applications:

Make sure your log file exists and give it the appropriate permissions.

Check the status of your daemon

or

Monitor it.

I’ll be using Monit to do the health checks now and then. I know I’ve set up the ‘respawn’ on my Upstart job, but Upstart will only check the PID file of the process. If a Node.js application is stuck in some state or another you have no clue if your application is still responding to, for instance, HTTP requests (which is irrelevant in some applications of course).

So we combine.

The Monit config file in this example does exactly what we want: do an HTTP request to a specific address and act upon a timeout: restart the Upstart job.

Make sure you start the Monit daemon and start the Monit test.

Monit will monitor the Upstart job and will restart it when a request to the site (with port) will fail.

You  can easily test this by stopping the Upstart job.

If you check the status of Monit

You will see that after a while it will say something like this.

If you check your site again, the Upstart job will be restarted.

And voila. You’re done.

Notes

  1. pieterm posted this