系统相关
首页 > 系统相关> > Nginx启停脚本

Nginx启停脚本

作者:互联网

Nginx启停脚本

vim nginx_tools.sh

#! /bin/sh
NGINX_HOME=/usr/local/nginx 

function help()
{
    echo "Usage: nginx_tools reload|restart|stop|start|term|show|relog"
    echo -e "\treload\t重新载入配置,主进程不停止,工作进程重启"
    echo -e "\trestart\t重启nginx"
    echo -e "\tstop\t停止nginx,但等待交易处理完毕"
    echo -e "\tstart\t启动nginx"
    echo -e "\tterm\t立即停止nginx"
    echo -e "\tshow\t查看nginx进程状态"
    echo -e "\trelog\t重新打开日志文件,不影响交易"
    exit 1
}

#如果参数1为空,则走help() 
if [ -z $1 ]; then
    help
fi
 
if [ -e $NGINX_HOME/logs/nginx.pid ]; then
    #如果nginx.pid文件存在
    nginx_pid=`cat $NGINX_HOME/logs/nginx.pid 2>/dev/null`
else
    #否则通过grep过滤出Nginx的进程号
    nginx_pid=$(ps -ef | grep -w `whoami` | grep -w "nginx:" | grep -w master | grep -vw grep | awk '{print $2}')
fi

#如果nginx进程不存在 
if [ -z $nginx_pid ]; then
    if [ "$1" == "reload" ] || [ "$1" == "restart" ] || [ "$1" == "relog" ]; then
        #等待输入,10s
        read -p "nginx未启动,是否启动:[Y/N] " -t 10 option
        if [ $? -ne 0 ]; then
            echo -e "\n超时未选择,自动退出" 
            exit 2
        fi
 
        if [ "$option" == "Y" ]; then
            $NGINX_HOME/sbin/nginx
            exit 0
        else
            exit 3
        fi
    elif [ "$1" == "stop" ] || [ "$1" == "term" ] || [ "$1" == "show" ] ; then
        echo "nginx已经停止"   
        exit 4
    fi
fi
 
case $1 in
    reload)
        kill -HUP $nginx_pid
        ;;
    restart)
        kill -QUIT $nginx_pid
        sleep 2
        $NGINX_HOME/sbin/nginx
        ;;
    stop)
        kill -QUIT $nginx_pid
        ;;
    start)
        if [ -z $nginx_pid ]; then
            $NGINX_HOME/sbin/nginx
        else
            echo "nginx已经启动"
        fi
        ;;
    term)
        kill -TERM $nginx_pid
        ;;
    show)
        date +"%F %X"
        ps -faux | grep -w `whoami` | grep -w "nginx:" | grep -v grep
        ;;
    relog)
        kill -USR1 $nginx_pid
        ;;
    *)
        echo "暂不支持的选项"
        help
        ;;
esac
 
exit 0

  

  

  

标签:脚本,grep,nginx,pid,echo,Nginx,启停,exit,fi
来源: https://www.cnblogs.com/cjzzz/p/16336228.html