不求甚解

此博客为个人学习之用,如与其他作品雷同,纯属巧合。

导航

nginx启停shell脚本

Posted on 2021-08-23 11:04  三年三班王小朋  阅读(225)  评论(0编辑  收藏  举报

启停脚本(nginx-1.19.5)

一、编辑脚本

vi /u01/nginx/nginxServer.sh

#!/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


 

 

三 systemd启动脚本

vi /usr/lib/systemd/system/nginx.service

[Unit]
Description=The Nginx HTTP Server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

 

 

如果不指定-c,nginx在启动时默认加载conf/nginx.conf文件。

 

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