leafsummer
8/4/2016 - 12:18 PM

nginx service command

nginx service command

#!/bin/sh
nginx=/usr/sbin/nginx
config=/etc/nginx/nginx.conf

case $1 in
    start)
        echo "Starting nginx"
        count=`ps -ef | grep 'nginx' | grep -v 'nginxd' | grep -v 'grep'`
        if [ ! -n "$count" ]; then
            $nginx -c $config
            if [ $? -ne 0 ]; then
                echo "Failed to start"
            else
                echo "Started"
            fi
        else
            echo "Nginx is running"
        fi
        ;;
    stop)
        echo "Stop nginx"
        count=`ps -ef | grep 'nginx' | grep -v 'nginxd' | grep -v 'grep'`
        if [ -n "$count" ]; then
            if ($nginx -s quit); then
                echo "Stopped"
            else
                echo "Failed to stop"
            fi
        else
            echo "Nginx is not running"
        fi
        ;;
    force-stop)
        echo "Force stop nginx"
        count=`ps -ef | grep 'nginx' | grep -v 'nginxd' | grep -v 'grep'`
        if [ -n "$count" ]; then
            if ($nginx -s stop); then
                echo "Stopped"
            else
                echo "Failed to stop"
            fi
        else
            echo "Nginx is not running"
        fi
        ;;
    reload)
        echo "Reload the configuration file"
        count=`ps -ef | grep 'nginx' | grep -v 'nginxd' | grep -v 'grep'`
        if [ -n "$count" ]; then
            if ($nginx -s reload); then
                echo "Reload configurion file finished"
            else
                echo "Failed to reload"
            fi
        else
            echo "Nginx is not running"
        fi
        ;;
    restart)
        echo "Restart Nginx"
        sh ${0} stop
        sleep 2
        sh ${0} start
        ;;
    show)
        ps -ef | grep 'nginx' | grep -v 'nginxd' | grep -v 'grep'
        ;;
    *)
        echo -n "Usage: $0 {start|stop|force-stop|reload|restart|show}"
        ;;
esac