用initd服务化nginx,支持centos6,7和ubuntu

  sre

vim /etc/init.d/nginx

#! /bin/bash
#2019-02-12  

PATH=/data/sbin:/data/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/data/nginx/sbin/NAME
CONFIGFILE=/data/nginx/conf/NAME.conf
PIDFILE=/data/nginx/logs/NAME.pid
if [ -s /usr/sbin/ss ]; then
    StatBin=/usr/sbin/ss
else
    StatBin=/usr/bin/netstat
fi


case "1" in
    start)
        echo -n "Starting NAME... "

        ifStatBin -tnpl | grep -q nginx;then
            echo "NAME (pid `pidofNAME`) already running."
            exit 1
        fi

        NGINX_BIN -cCONFIGFILE

        if [ "?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    stop)
        echo -n "StopingNAME... "

        if ! StatBin -tnpl | grep -q nginx; then
            echo "NAME is not running."
            exit 1
        fi

        NGINX_BIN -s stop

        if [ "?" != 0 ] ; then
            echo " failed. Use force-quit"
            0 force-quit
        else
            echo " done"
        fi
        ;;

    status)
        ifStatBin -tnpl | grep -q nginx; then
            PID=`pidof nginx`
            echo "NAME (pidPID) is running..."
        else
            echo "NAME is stopped."
            exit 0
        fi
        ;;

    force-quit|kill)
        echo -n "TerminatingNAME... "

        if ! StatBin -tnpl | grep -q nginx; then
            echo "NAME is is stopped."
            exit 1
        fi

        kill `pidof NAME`

        if [ "?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    restart)
        0 stop
        sleep 10 start
        ;;

    reload)
        echo -n "Reload service NAME... "

        ifStatBin -tnpl | grep -q nginx; then
            NGINX_BIN -s reload
            echo " done"
        else
            echo "NAME is not running, can't reload."
            exit 1
        fi
        ;;

    configtest)
        echo -n "Test NAME configure files... "NGINX_BIN -t
        ;;

    *)
        echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"
        exit 1
        ;;

esac

chmod +x /etc/init.d/nginx
chkconfig nginx on

LEAVE A COMMENT

Captcha Code