#!/bin/bash
# chkconfig: - 85 15
NGINX="/usr/local/nginx/sbin/nginx"
PROG=$(basename $NGINX)
LOCKFILE="/usr/local/nginx/nginx.lock"
PIDFILE="/usr/local/nginx/nginx.pid"
CONF="/usr/local/nginx/conf/nginx.conf"
case "$1" in
    start)
        if [ -f $$PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed."
        else
                echo "Starting $PROG..."
                $NGINX -c $CONF
                [ $? -eq 0 ] && touch ${LOCKFILE} sleep 4 && touch ${PIDFILE}
                echo "Done"
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running."
        else
                echo "Stopping $PROG..."
                killall -s QUIT $PROG
                [ $? -eq 0 ] && rm -f ${LOCKFILE} ${PIDFILE}
                sleep 1
                echo "Done"
        fi
        ;;
    status)
        if [ ! -f $PIDFILE ]
        then
            echo "Nginx is not running now!"
        else
            echo "Nginx is running now!"
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    reload)
        echo "Reloading $PROG..."
        killall -s HUP $PROG
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart|reload}"
        exit 2
        ;;
esac