Nginx启动脚本

Posted 谭普利特

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx启动脚本相关的知识,希望对你有一定的参考价值。

#!/bin/bash
#author Template
#Time 2018-06-30 22:07
# chkconfig  2345 40 98
# description nginx start/stop script

prefix=/usr/local/nginx

Nginx_Bin=${prefix}/sbin/nginx
Nginx_Pid=${prefix}/logs/nginx.pid
Nginx_Conf=${prefix}/conf/nginx.conf

. /etc/init.d/functions

function start(){
    
    if [ ! -f ${Nginx_Pid} ];then
    #if [ `netstat -tlunp  | grep nginx | wc -l` -eq 0 ];then
        ${Nginx_Bin}
        RETVAL=$?
        
        if [ ${RETVAL} -eq 0 ];then
            action "Nginx started" /bin/true
        else
            action "Nginx started" /bin/false
        fi
        return ${RETVAL}
    else
        echo "Nginx is running" 
        return 0
    fi
}

function stop(){

    if [ -f ${Nginx_Pid} ];then

        ${Nginx_Bin} -s stop
        RETVAL=$?
        if [ ${RETVAL} -eq 0 ];then
            action "Nginx stoped" /bin/true
            return ${RETVAL}
        else
            action  "Nginx stoped" /bin/false
            return ${RETVAL}
        fi
    else
        echo  "Nginx is not running" 
        return 1

    fi
}

function reload(){
    ${Nginx_Bin} -t -q -c ${Nginx_Conf} &> /dev/null
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
        ${Nginx_Bin} -s reload
        RETVAL=$?
        [ ${RETVAL} -eq 0 ] && action "Nginx reload" /bin/true || action "Nginx reload" /bin/false
    else
        action "Reload" /bin/false
           echo "Please check your configuration"
        return ${RETVAL}
    fi

}

case $1 in

    start)
        start
        RETVAL=$?
        ;;
    stop)
        stop
        RETVAL=$?
        ;;
    restart)
        stop
        sleep 1
        start
        RETVAL=$?
        ;;

    reload)
        reload
        RETVAL=$?
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|reload}"
        exit 1
esac
exit $RETVAL

 

以上是关于Nginx启动脚本的主要内容,如果未能解决你的问题,请参考以下文章

sh Unbounce脚本片段,用于在零停机时间内重新启动HAProxy

Shell脚本切割日志

nginx启动脚本

nginx配置快捷启动service

Nginx 启动脚本/重启脚本

nginx编译安装服务启动脚本在哪