title: Restart Node.js App with init.d
body: <p>I'd like to have an init.d daemon restart my node.js app if it crashes. This script starts/stops my node app. I've had no luck getting it to restart the app if it crashes.  </p>
  
  <p>I'm running under CentOS. What am I missing?</p>
  
  <pre><code>#!/bin/sh
  . /etc/rc.d/init.d/functions
  
  USER="rmlxadmin"
  DAEMON="/usr/bin/nodejs"
  ROOT_DIR="/home/rmlxadmin"
  
  SERVER="$ROOT_DIR/my_node_app.js"
  LOG_FILE="$ROOT_DIR/app.js.log"
  
  LOCK_FILE="/var/lock/subsys/node-server"
  
  do_start()
  {
          if [ ! -f "$LOCK_FILE" ] ; then
                  echo -n $"Starting $SERVER: "
                  runuser -l "$USER" -c "$DAEMON $SERVER &gt;&gt; $LOG_FILE &amp;" &amp;&amp; echo_success || echo_failure
                  RETVAL=$?
                  echo
                  [ $RETVAL -eq 0 ] &amp;&amp; touch $LOCK_FILE
          else
                  echo "$SERVER is locked."
                  RETVAL=1
          fi
  }
  do_stop()
  {
          echo -n $"Stopping $SERVER: "
          pid=`ps -aefw | grep "$DAEMON $SERVER" | grep -v " grep " | awk '{print $2}'`
          kill -9 $pid &gt; /dev/null 2&gt;&amp;1 &amp;&amp; echo_success || echo_failure
          RETVAL=$?
          echo
          [ $RETVAL -eq 0 ] &amp;&amp; rm -f $LOCK_FILE
  }
  
  case "$1" in
          start)
                  do_start
                  ;;
          stop)
                  do_stop
                  ;;
          restart)
                  do_stop
                  do_start
                  ;;
          *)
                  echo "Usage: $0 {start|stop|restart}"
                  RETVAL=1
  esac
  exit $RETVAL
  </code></pre>