Posts tagged: node.js

Restart Forever.js After Reboot

I’m amid migrating from a Dreamhost shared server to a VPS. A benefit of this is the ability to have long running processes like node.js. I’m also migrating the back end for Menu Line George to Parse.com. My v1 process for recording the George calls was a kludge of scripts running on my home desktop to call via Skype. This was more a proof-of-concept than a long term solution. Now I’ll be using Twilio + node.js + Parse as my permanent solution, significantly reducing how much I’ll need to think about it on a daily basis.

The simplest way I’ve found to keep a node script running as a service is forever.js. If you’re new to node, I suggest checking it out. Otherwise, I’m sure you’ve heard of it.

I fired it up yesterday and it worked like a champ. That is, of course, until my VPS rebooted overnight for some reason and the call wasn’t made this morning. I found a handy little guide over at Hack Sparrow for creating a cron job to restart your node script with forever when the machine reboots.

First create a shell script like this one (I called mine app-starter.sh):

#!/bin/sh

if [ $(ps aux | grep $USER | grep node | grep -v grep | wc -l | tr -s "\n") -eq 0 ]
then
        export NODE_ENV=production
        export PATH=/usr/local/bin:$PATH
        forever start ~/example/app.js > /dev/null
fi

Then make the script executable:

chmod 700 ~/app-starter.sh

Now make cron run the script when the system is rebooted. Add this to your crontab (via crontab –e):

@reboot ~/app-starter.sh >> cron.log 2>&1

For the uninitiated like myself, use CTRL+K,X to save.

Thanks to Hack Sparrow for taking the time to share this.

via Hack Sparrow.