这是控制nginx服务的脚本文件,包括控制nginx的启动、重启、停止、平滑重启、对配置文件的额检查。

[root@localhost ~]# cat nginx.sh 

#!/bin/env bash

# description:nginx server   ###必须加描述

# nginx - this script is used to control nginx service

# processname nginx

# chkconfig: - 85 15

# edit by sun

# date 2014-07-03

# email address 1305627792@qq.com


nginx="/usr/local/nginx/sbin/nginx"

prog="nginx"

conf_file="/usr/local/nginx/conf/nginx.conf"


start() {

    if [ `pgrep $prog | wc -l` -eq 0 ];then

        if [ -x $nginx ] && [ -f $conf_file ];then

            $nginx -c $conf_file

            ret=$?

                if [ $ret -eq 0 ];then

                    echo "$prog start       successed"

                else

                    echo "$prog start       failed"

                fi

        else

            echo "$prog config file not exist"

        fi

    else

        echo "$prog is already started ... "

fi

}


stop() {

    if [ `pgrep $prog | wc -l` -ne 0 ];then

        killall -9 $prog

        ret=$?

            if [ $ret -eq 0 ];then

                echo "$prog stop       successed"

            else

                echo "$prog stop   failed"

            fi

    else

        echo "$prog is already stopped ..."

    fi

}


restart() {

    stop

    sleep 2

    start

}


reload() {

    if [ `pgrep $prog | wc -l` -ne 0 ];then

        pid=`ps -ef | grep $prog | grep master | awk '{print $2}'`

            if [ -x $nginx ] && [ -f $conf_file ];then

                kill -HUP $pid

                ret=$?

                    if [ $ret -eq 0 ];then

                        echo "$prog reload  successed"

                    else

                        echo "$prog reload    failed"

                    fi

            else

                echo "$prog config file is not exist"

            fi

    else

        echo "$prog is stopped, please start $prog first ..."

    fi

}


check() {

    if [ -x $nginx ] && [ -f $conf_file ];then

        $nginx -t -c $conf_file

        ret=$?

            if [ $ret -eq 0 ];then

                echo "$prog check successed"

            else

                echo "$prog check failed"

            fi

    else

        echo "$prog program or config file not exit!"

    fi

}


case $1 in

    start)

            start 

            ;;

    stop)

            stop

            ;;

    restart)

            restart

            ;;

    reload)

            reload

            ;;

    check)

            check

            ;;

    *)

        echo "Usage: $0 {start|stop|restart|reload|check}"

esac

运行bash nginx.sh start等操作即可。