nginx启停脚本

Posted 拾日迂南

tags:

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

[root@manager case]# cat case3.sh 
#!/bin/bash
#********************************************************************
#Author     : Oldxu
#Date       : 2021-09-25
#FileName   : case3.sh
#Description: The test script
#********************************************************************

#ngin启停脚本
. /etc/init.d/functions

read -p "你想执行的操作是[ start | stop | restart ]: " action

#定义变量
start_nginx() {
    #首先判断nginx是否正在运行
    nginx_status=$(ps aux |grep nginx | grep -v grep | wc -l)
    if [ ${nginx_status} -eq \'0\' ];then
        systemctl start nginx
        nginx_status=$(ps aux |grep nginx | grep -v grep | wc -l)
        if [ ${nginx_status} -ne \'0\' ];then
            action "nginx成功启动...." /bin/true
        else
            action "nginx启动失败...." /bin/false
            exit
        fi
    else
        action "nginx 已经启动,无需再次启动....." /bin/false
        exit
    fi
}
stop_nginx() {
    #首先判断nginx是否正在运行
    nginx_status=$(ps aux |grep nginx | grep -v grep | wc -l)
    if [ ${nginx_status} -ne \'0\' ];then
        systemctl stop nginx &>/dev/null
        nginx_status=$(ps aux |grep nginx | grep -v grep | wc -l)
        if [ ${nginx_status} -eq \'0\' ];then
            action "nginx停止成功...." /bin/true
        else
            action "nginx停止失败...." /bin/false
            exit
        fi
    else
        action "nginx已经停止,无需再次停止....." /bin/false
        exit
    fi
}
restart_nginx() {
        nginx_status=$(ps aux |grep nginx | grep -v grep | wc -l)
            if [ ${nginx_status} -eq \'0\' ];then
            start_nginx &>/dev/null
            action "nginx重启完毕" /bin/true
        else
            stop_nginx &>/dev/null
            start_nginx &>/dev/null
            action "nginx重启完毕" /bin/true
        fi
}

case ${action} in
    start)
        start_nginx
    ;;
    stop)
        stop_nginx
    ;;
    restart)
        restart_nginx
    ;;
    *)
        echo "你输入的内容有误,退出....."
        exit
esac

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

nginx启停脚本

nginx启停脚本

Nginx配置文件详解以及Nginx的启停控制,这里是精华

nginx的启停控制命令详解

K8S 应用启停通用脚本

NGINX的启停命令以及动态加载配置文件的命令