ranetoの起動スクリプト

amazon linuxでのranetoの起動スクリプトです。

#!/bin/sh

#
# chkconfig: 35 99 99
# description: Node.js

# processname: node
# config: /etc/nodejs/daemon
# pidfile: /var/run/nodejs.pid
. /etc/rc.d/init.d/functions

USER="root"
NODE="/usr/bin/node"

prog="nodejs"
lockfile="/var/lock/subsys/nodejs"
pidfile="/var/run/nodejs.pid"

if [ -f /etc/nodejs/daemon ]; then
  source /etc/nodejs/daemon
fi

if [ "${ROOT_DIR}" = "" ]; then
  echo "please specify ROOT_DIR in /etc/nodejs/daemon"
  exit 1
fi

if [ "${SERVER}" = "" ]; then
  SERVER="$ROOT_DIR/bin/www"
fi

if [ "${LOG_FILE}" = "" ]; then
  LOG_FILE="$ROOT_DIR/www.log"
fi



do_start()
{
        if [ ! -f "$lockfile" ] ; then
                echo -n $"Starting $SERVER: "
                runuser -l "$USER" -c "cd $ROOT_DIR || exit 1; $NODE $SERVER >> $LOG_FILE &" && echo_success || echo_failure
                RETVAL=$?
                PID=`ps -aefw | grep "$NODE $SERVER" | grep -v " grep " | awk '{print $2}'`
                echo "${PID}" > ${pidfile}
                echo
                [ $RETVAL -eq 0 ] && touch $lockfile
        else
                echo "$SERVER is locked."
                RETVAL=1
        fi
}

do_stop()
{
        echo -n $"Stopping $prog: "
        killproc $prog
        echo ""
        RETVAL=$?
        [ $RETVAL -eq 0 ] && rm -f $lockfile
        return $RETVAL
}

case "$1" in
        start)
                do_start
                ;;
        stop)
                do_stop
                ;;
        status)
                status $prog
                RETVAL=$?
                ;;
        restart)
                do_stop
                do_start
                ;;
        *)
                echo "Usage: $0 {start|stop|status|restart}"
                RETVAL=1
esac

exit $RETVAL