shell脚本-nginx启动管理脚本

#Decription:
#Author: Batac_lzp
#Created Time: 2020/11/15 14:47
#nginx service manage script

# 变量  下方函数中会使用
#variables
nginx_install_doc=/application/nginx
nginxd=$nginx_install_doc/sbin/nginx
pid_file=$nginx_install_doc/logs/nginx.pid


# Source function library
# 加载系统函数
if [ -f /etc/init.d/functions ];then
        . /etc/init.d/functions
else
        echo "not found file /etc/init.d/functions"
        exit
fi
if [ -f $pid_file ];then
        # 获取进程号 nginx启动后会将进程号写入文件中 可获取进程号做进一步判
        nginx_process_id=`cat $pid_file`                                
        # 根据进程号进行判断该进程是否存在
        nginx_process_num=`ps aux | grep $nginx_process_id|grep -v "grep" |wc -l`
fi 

# function
# 启动函数
start () {
# 如果nginx没有启动则直接启动, 否则报错, 已经启动, 则什么都不做
# 先判断进程文件是否存在
# 只要进程存在 则 nginx_process_num 不小于0
if [ -f $pid_file ]&& [ $nginx_process_num -ge 1 ];then
        echo "nginx running..."
else
        # 否则对应的进程不存在  即需要启动nginx
        if [ -f $pid_file ] && [ $nginx_process_num -lt 1 ];then
                rm -f $pid_file
                # 启动nginx daemon:是一个系统函数库中的函数
                echo "nginx start `daemon $nginxd`"
                # action "nginx start" $nginxd
        fi
# 文件也不存在 进程也不存在 则重启nginx 
echo "nginx start `daemon $nginxd`"
# action "nginx start" $nginxd
fi
}

# 关闭nginx服务
stop () {
# 进程ID文件存在 并且 进程也存在 则直接关闭nginx
if [ -f $pid_file ]&&[ $nginx_process_num -ge 1 ];then
        # if killall -s QUIT $nginxd;then
        # echo -e "nginx \033[31m [stop] \033[0m `killall -s QUIT       nginx`"
        action "nginx stop" killall -s QUIT nginx
        rm -f $pid_file
else
        # echo -e "nginx stop \033[31m [fail] \033[0m `killall -s QUIT nginx 2>/dev/null`"
         action "nginx stop" killall -s QUIT nginx 2>/dev/null
fi                                                              
}

# 重启nginx服务
restart () {
        # 先重启
        stop
        # 休息 1 秒钟
        sleep 1
        # 启动nginx
        start
        #echo "restart"
}

reload () {
        if [ -f $pid_file ]&&[ $nginx_process_num -ge 1 ];then
                action "nginx reload" killall -s HUP nginx
        else
                action "nginx reload" killall -s HUP nginx 2>/dev/null
        fi
        #echo "reload"
}

status () {
        if [ -f $pid_file ]&&[ $nginx_process_num -ge 1 ];then
                echo -e "nginx \033[32m [running] \033[0m"
        else
                echo -e "nginx \033[31m [stop] \033[0m" 
        fi
        #echo "status"
}

#根据输入参数 调用不容的功能函数
#callable
case $1 in
        start) start;;
        stop) stop;;
        restart) restart;;
        reload) reload;;
        status) status;;
        "") echo -e "请输入参数: \033[31m start | stop | restart | reload | status \033[0m"
esac

猜你喜欢

转载自blog.csdn.net/Batac_Lee/article/details/109705712