1 init.d script for haste server
ambar seksena edited this page 2013-08-24 12:20:22 -07:00

This is a basic init.d script for haste-server. This has been tested on a CentOS 5.5 ec2 instance.

#!/bin/sh
#
# this script: /etc/init.d/hasteserver
#
# modified from: https://gist.github.com/nariyu/1211413
# 
# description: Node.js /var/www/hastebin/server.js


. /etc/rc.d/init.d/functions

USER="ec2-user"

DAEMON="/usr/local/bin/node"
ROOT_DIR="/var/www/hastebin"

SERVER="$ROOT_DIR/server.js"
LOG_FILE="$ROOT_DIR/server.js.log"

LOCK_FILE="/var/lock/subsys/node-server"

do_start()
{
        if [ ! -f "$LOCK_FILE" ] ; then
                echo -e $"Starting $SERVER ...\n"
                cd $ROOT_DIR
                #runuser -l "$USER" -c "$DAEMON $SERVER >> $LOG_FILE &" && echo_success || echo_failure
                nohup $DAEMON $SERVER &  >> $LOG_FILE && echo_success || echo_failure
                RETVAL=$?
                echo
                [ $RETVAL -eq 0 ] && sudo touch $LOCK_FILE
        else
                echo "$SERVER is locked. LOCK_FILE: $LOCK_FILE"
                RETVAL=1
        fi
}
do_stop()
{
        echo $"Stopping nodejs $SERVER ..."
        #pid=`ps -aefw | grep "$DAEMON $SERVER" | grep -v " grep " | awk '{print $2}'`
        #kill -9 $pid > /dev/null 2>&1 && echo_success || echo_failure
        pkill -f server.js
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && sudo 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

Copy and paste it into /etc/init.d/hasteserver and then edit these variables in the script according to your installation of haste-server:

  • USER
  • DAEMON
  • ROOT_DIR
  • LOG_FILE

Then sudo chmod a+x /etc/init.d/hasteserver

Now you should be able to do service hasteserver start and service hasteserver stop.

You can then use chkconfig to start hastebin automatically on every reboot like this:

sudo chkconfig --add hasteserver
sudo chkconfig --level 345 hasteserver on
sudo chkconfig --list hasteserver