1. 程式人生 > 實用技巧 >shell nginx指令碼

shell nginx指令碼

nginx編譯安裝 啟動指令碼

[root@m01 /scripts]# vim /scripts/nginx.sh
#!/usr/bin/env bash
#Author:xiaolang
#Blog:https://www.cnblogs.com/xiaolang666
#Time:2020-11-20  23:24:44
#Name:nginx.sh
#Version: 1.0
#Discription: nginx 啟動指令碼  


[ $# -ne 1 ] && echo "Usage: $0 {start|stop|restart|reload|status}" && exit

ps aux| grep [n]ginx | grep [m]aster &> /dev/null
start_flag=`echo $?`

function start(){
    if [ $start_flag -eq 0 ];then
        echo 'nginx 啟動成功'
        return 0
    else
        /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf &> /dev/null
        if [ $? -eq 0 ];then
            echo 'nginx 啟動成功'
            return 0
        else 
            echo 'nginx 啟動失敗'
            return 1
        fi
    fi
}

function stop(){
     ps aux| grep [n]ginx | grep [m]aster &> /dev/null
     if [ $? -ne 0 ];then
         echo 'nginx 關閉成功'
         return 0
     else
         /usr/local/nginx/sbin/nginx -s stop &> /dev/null
         sleep 2
         ps aux| grep [n]ginx | grep [m]aster &> /dev/null
         if [ $? -ne 0 ];then
             echo 'nginx 關閉成功'
             return 0
         else
             echo 'nginx 關閉失敗'
             return 1
         fi
     fi
}

function reload(){
    /usr/local/nginx/sbin/nginx -s reload &> /dev/null
    if [ $? -eq 0 ];then
        echo "nginx 配置檔案過載成功"
    else
        echo "nginx 配置檔案過載失敗"
    fi       
}

function status(){
    ps aux| grep [n]ginx | grep [m]aster &> /dev/null
    if [ $? -eq 0 ];then
        echo 'nginx 正在執行'
    else
        echo 'nginx 已經停止'
    fi
}

case $1 in
'start')
    start
    ;;
'stop')
    stop
    ;;
'restart')
    stop &> /dev/null
    stop_status_code=`echo $?`
    start &> /dev/null
    start_status_code=`echo $?`
    if [ $stop_status_code -eq $start_status_code ];then
        echo 'nginx 重啟成功'
    else
        echo 'nginx 重啟失敗'
    fi
    ;;
'reload')
    reload
    ;;
'status')
    status   
    ;;
*)
    echo "Usage: $0 {start|stop|restart|reload|status}"
esac