启停脚本(nginx-1.19.5)

一、编辑脚本

vim /u01/nginx

#!/bin/bash
# chkconfig: 2345 85 15
# description:Nginx Server  
# auther by wangxp

# 变量定义
NGINX_HOME="/u01/nginx"
PROG="Nginx"
#变量运用
NGINX_SBIN="$NGINX_HOME/sbin/nginx"
NGINX_CONF="$NGINX_HOME/conf/nginx.conf"
NGINX_PID="$NGINX_HOME/logs/nginx.pid"

. /etc/rc.d/init.d/functions

if test ! -f $NGINX_SBIN
then
    echo "$NGINX_NAME startup: $NGINX_SBIN not exists! "
    exit
fi

function getPid {
if test -f $NGINX_PID
then
PID=`cat $NGINX_PID`
else
PID=0
fi
}

getPid
case "$1" in
    start)
        if [ $PID -eq  0 ];then
                $NGINX_SBIN -c $NGINX_CONF
                ret=$?
                if [ $ret -eq 0 ]; then
                        echo "$PROG start success"
                else
                        echo "$PROG start failed"
                fi
        else
                echo   "$PROG exist"
        fi
        ;;
    stop)
        if [ $PID -eq  0 ];then
                echo  "$PROG not exist"
        else
                $NGINX_SBIN -s quit
                ret=$?
                if [ $ret -eq 0 ];then
                        echo "$PROG stop success"
                else
                        echo "$PROG stop failed"
                fi
        fi
        ;;
    restart)
        if [ $PID -eq  0 ];then
                $NGINX_SBIN -c $NGINX_CONF
                ret=$?
                if [ $ret -eq 0 ]; then
                        echo "$PROG restart success"
                else
                        echo "$PROG restart failed"
                fi
        else
                $NGINX_SBIN -s quit
                ret=$?
                if [ $ret -eq 0 ]; then
                        echo "$PROG stop success"
                        sleep 1
                        $NGINX_SBIN -c $NGINX_CONF
                        ret=$?
                        if [ $ret -eq 0 ]; then
                                echo "$PROG restart success"
                        else
                                echo "$PROG restart failed"
                        fi
                else
                        echo "$PROG stop failed"
                fi
        fi
        ;;
    check|chk)
        $NGINX_SBIN -c $NGINX_CONF -t
        ;;
    reload)
        $NGINX_SBIN -c $NGINX_CONF -s reload
        ret=$?
        if [ $ret -eq 0 ]; then
                        echo “$PROG reload success”
        else
                        echo “$PROG reload failed”
        fi
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|reload|check}"
        exit 1
esac

二、开机启动

[root@localhost local]# cp /u01/nginx/nginx.sh /etc/init.d/nginx
[root@localhost local]# chkconfig --add nginx
[root@localhost local]# systemctl start nginx

查看启动列表
[root@localhost local]# systemctl --list

nginx启停shell脚本_开机启动