首页 > 代码库 > 利用BashShell编写Nginx服务控制脚本

利用BashShell编写Nginx服务控制脚本

使用源码包安装的Nginx没办法使用"service nginx start"或"/etc/init.d/nginx start"进行操作和控制,所以写了以下的服务控制脚本。

可以使用的选项有:

start     启动

stop     停止

reload   重载

restart    重启

status    状态

test    检查配置文件

 

1、创建脚本文件并添加执行权限

# touch /etc/init.d/nginx# chmod +x /etc/init.d/nginx

 

2、编写脚本内容

  1 #!/bin/bash  2 # chkconfig: - 85 15  3 # description: Nginx server control script  4 # processname: nginx  5 # config file: /usr/local/nginx/conf/nginx.conf  6 # pid file: /usr/local/nginx/logs/nginx.pid  7 #   8 # eastmoney public tools  9 # version: v1.0.0 10 # create by XuHoo, 2016-9-14 11 #  12  13 # source function library 14 . /etc/rc.d/init.d/functions 15  16 NGINX_NAME="nginx" 17 NGINX_PROG="/usr/local/sbin/nginx" 18 NGINX_PID_FILE="/usr/local/nginx/logs/nginx.pid" 19 NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 20 NGINX_LOCK_FILE="/var/lock/subsys/nginx.lock" 21  22 # check current user 23 [ "$USER" != "root" ] && exit 1 24  25 start() { 26     status 27         if [[ $? -eq 0 ]]; then 28             echo $"Nginx (PID $(cat $NGINX_PID_FILE)) already started." 29             return 1 30         fi 31     echo -n $"Starting $NGINX_NAME: " 32         daemon $NGINX_PROG -c $NGINX_CONF_FILE 33         retval=$? 34         echo 35     [ $retval -eq 0 ] && touch $NGINX_LOCK_FILE 36     return $retval 37 } 38  39 stop() { 40     status 41         if [[ $? -eq 1 ]]; then 42             echo "Nginx server already stopped." 43             return 1 44         fi 45     echo -n $"Stoping $NGINX_NAME: " 46         killproc $NGINX_PROG 47         retval=$? 48         echo 49     [ $retval -eq 0 ] && rm -f $NGINX_LOCK_FILE 50     return $retval 51 } 52  53 restart() { 54     stop 55         sleep 1 56     start 57     retval=$? 58     return $retval 59 } 60  61 reload() { 62     echo -n $"Reloading $NGINX_NAME: " 63         killproc $NGINX_PROG -HUP 64         retval=$? 65         echo 66     return $retval 67 } 68  69 status() { 70     netstat -anpt | grep "/nginx" | awk {print $6} &> /dev/null 71         if [[ $? -eq 0 ]]; then 72             if [[ -f $NGINX_LOCK_FILE ]]; then 73                 return 0 74             else 75                 return 1 76             fi 77         fi 78     return 1 79 } 80  81 _status() { 82     status 83         if [[ $? -eq 0 ]]; then 84             state=`netstat -anpt | grep "/nginx" | awk { print $6 }` 85             echo $"Nginx server status is: $state" 86         else 87             echo "Nginx server is not running" 88         fi 89 } 90  91 test() { 92     $NGINX_PROG -t -c $NGINX_CONF_FILE 93         retval=$? 94     return $retval 95 } 96  97 case "$1" in 98     start) 99         start100         ;;101     stop)102         stop103         ;;104     reload)105         reload106         ;;107     restart)108         restart109         ;;110     status)111         _status112         ;;113     test)114         test115         ;;116     *)117         echo "Usage: { start | stop | reload | restart | status | test }"118         exit 1119 esac

 

3、将脚本添加到系统服务并设置开机启动

# chkconfig --add nginx# chkconfig --level 3 nginx on

 

4、测试脚本是否能够执行

运行命令: service nginx start 或 /etc/init.d/nginx start

 

利用BashShell编写Nginx服务控制脚本