利用shell开发nginx启动脚本

一、nginx部署说明

PID文件:/data/apps/nginx/logs/nginx.pid
启动方式:/data/apps/nginx/sbin/nginx
停止方式:/data/apps/nginx/sbin/nginx -s stop
平滑重启:/data/apps/nginx/sbin/nginx -s reload

二、nginx启动脚本

脚本名称:nginxd

存放位置:/data/apps/nginx/

#!/bin/bash
#
#
# Define variables
RETVAL=0
Pid="/data/apps/nginx/logs/nginx.pid"
Start="/data/apps/nginx/sbin/nginx"
Stop="/data/apps/nginx/sbin/nginx -s stop"
Reload="/data/apps/nginx/sbin/nginx -s reload"

# Determine the user to execute
if [ $UID -ne $RETVAL ];then
   echo "Must be root to run scripts"
   exit 1
fi

# Load the local functions library
[ -f /etc/init.d/functions ] && source /etc/init.d/functions

# Define functions
start(){
        if [ ! -f "$Pid" ];then
           $Start
           RETVAL=$?
           if [ $RETVAL -eq 0 ];then
              action "Start nginx service" /bin/true
             else
              action "Start nginx service" /bin/false
           fi
          else
           echo "Nginx is running"
           exit 1
        fi
        return $RETVAL
}

stop(){
        if [ -f "$Pid" ];then
           $Stop
           RETVAL=$?
           if [ $RETVAL -eq 0 ];then
              action "Stop nginx service" /bin/true
             else
              action "Stop nginx service" /bin/false
           fi
          else
           echo "Nginx is not running"
           exit 1
        fi
        return $RETVAL
}

reload(){
        if [ -f "$Pid" ];then
           $Reload
           RETVAL=$?
           if [ $RETVAL -eq 0 ];then
              action "Reload nginx service" /bin/true
             else
              action "Reload nginx service" /bin/false
           fi
         else
           echo "Nginx is not running"
           exit 1
        fi
        return $RETVAL
}

status(){
        if [ -f "$Pid" ];then
           echo "Nginx is running"
           exit 0
          else
           echo "Nginx is not running"
           exit 0
        fi
        return $RETVAL
}

# Case call functions
case "$1" in
  start)
        start
        RETVAL=$?
        ;;
  stop)
        stop
        RETVAL=$?
        ;;
  reload)
        reload
        RETVAL=$?
        ;;
  status)
        status
        RETVAL=$?
        ;;
  *)
        echo "USAGE:$0 {start|stop|reload|status}"
        exit 1
esac

# Scripts return values
exit $RETVAL

三、加入开机自启动

我一般放在/etc/rc.local文件中,并给/etc/rc.local文件更改权限为744

[root@node33 ~]# echo -ne "\n# Boot start nginx service.USER:chenliang TIME:$(date +%F)\n/data/apps/nginx/nginxd start\n" >>/etc/rc.local
[root@node33 ~]# tail -2 /etc/rc.local 
# Boot start nginx service.USER:chenliang TIME:2020-02-18
/data/apps/nginx/nginxd start

 

猜你喜欢

转载自www.cnblogs.com/chenliangc/p/11480241.html