Keeping Node Alive

Exceptions tend to be nasty problems regardless of language or framework - this is especially true for Node.

If you're unfamiliar with how Node works, when your site crashes, it crashes hard. In order to keep your site alive you need to either have flawless code (not me), or understand how to restart your website automatically (not me).

To solve this problem, I attempted to install forever using npm which failed over and over. I read a handful of reported issues which all essentially suggested the same fix - update npm. Unfortunately, I was already using the most recent version and it still failed to work.

So, with my terribly limited shell script knowledge, I created a script named auto.sh that did the following...

# monitor the site
nodemon --exitcrash app.js

# give a moment to end the script
echo Press CTRL+C to exit...
sleep 1

# restart the script
./auto.sh

Hmm.... Is that it?

Now, using screen I can start my script which will keep the Node site running.

But, I don't know if this is a real solution or not. It seems to do exactly what I need but I might be going about it wrong.

In any case, my site has been running great and I haven't had to touch it again since then.

March 8, 2012

Keeping Node Alive

Thoughts on a simple way to keep Node sites running.