nginx 启动服务脚本(shell)

#!/bin/bash
ng_path='/usr/local/nginx'
ng_bin="$ng_path/sbin/nginx"
ng_pid="$ng_path/logs/nginx.pid"

#Configure Script Variables
red='\033[31m'
green='\033[32m'
end='\033[0m'

#check
if [ ! -d $ng_path ];then
     echo -e "${red}ERROR:${end} $ng_path Directory does not exist !!"
     exit 1
elif [ ! -f $ng_bin ];then
     echo -e "${red}ERROR:${end} $ng_bin File does not exist !!"
     exit 1
fi

#Service state function
ng_start() {
     if [ -f $ng_pid ];then
        echo -e "${green}Service is running PID is `cat $ng_pid` ${end}"
     else
	$ng_bin &> /dev/null
        if [ $? -eq 1 ];then
           echo -n -e "${red}ERROR ${end}" && $ng_bin
        else
           echo -e "${green}Service startup succeeded PID is `cat $ng_pid` ${end}"
        fi
     fi
     }

ng_stop() {
     $ng_bin -s stop &> /dev/null
        if [ $? -eq 1 ];then
           echo -n -e "${red}ERROR ${end}" && $ng_bin -s stop
        else
           rm -rf $ng_pid
           echo -e "${green}Service is down${end}"
        fi
     }

ng_check() {
     $ng_bin -t
     }

ng_status() {
     if [ -f $ng_pid ];then
        echo -e "${green}Service is running PID is `cat $ng_pid` ${end}"
     else 
        echo -e "${red}Service is not running${end}" 
     fi
     }

ng_reload() {
     $ng_bin -s reload &> /dev/null
        if [ $? -eq 1 ];then
           echo -n -e "${red}ERROR ${end}" && $ng_bin -s reload
        else
           echo -e "${green}Service load succeeded${end}"
        fi
     }

#================================
case $1 in 
     start)
          ng_start;;
     stop)
          ng_stop;;
     status)
          ng_status;;
     check)
          ng_check;;
     reload)
          ng_reload;;
     restart)
          ng_stop && ng_start;;
     *)
          echo "Usage: $0 {start|stop|restart|reload|status|check}"
esac

猜你喜欢

转载自blog.csdn.net/qq_31755183/article/details/85685184