13 March 2012

I run CentOS for my production servers, and generally like to manage my services via proper init scripts. Over the years I’ve run into situations where I couldn’t locate a decent init script for a service I want to deploy, and end up rolling my own. I’m no shell scripting wizard, but they work well for me; it seems like something worth sharing so others don’t have to reinvent the wheel.

Today, I went hunting for an init script that I could use to manage a NodeJS application, and while I found a Debian-based one, no Redhat-style one was forthcoming. I tweaked the Debian one I found to work on a Redhat-style system. The script below should work fine for any Redhat/Redhat clone server. You’ll need to take care of a few things after you get the script:

  • Install the forever package so it’s in your PATH, which is easily accomplished via npm install -g forever if you have NPM installed.
  • Edit the variables in the APPLICATION section to suit your needs.
  • Put the script into /etc/init.d and name it something meaningful, I go with node-[appname].
  • Make sure the it’s executable.
  • If you want to have it automagically start/stop on startup/shutdown, activate it with chkconfig, like chkconfig --add [scriptname].
  • Manage it manually via the service app, eg. service [scriptname] start.

This script should be reusable to run more than one node app, provided that:

  • There’s one uniquely named script per node app.
  • The node apps are started on different ports.