nginx啟動腳本編寫及設置開機自啟動
如果機器是Centos 7的,此腳本和設置開機自啟動方法不適用。
首先確保nginx配置文件中:有pid目錄
pid logs/nginx.pid;
1.1 編寫nginx啟動腳本
[root@devops01-web-53 ~]# cd /server/scripts [root@devops01-web-53 scripts]# vim nginx.sh #!/bin/bash [ -f /etc/init.d/functions ] && . /etc/init.d/functions pidfile=/application/nginx/logs/nginx.pid Start_Nginx(){ if [ -f $pidfile ];then echo "Nginx is running" else /application/nginx/sbin/nginx &>/dev/null action "Nginx is Started" /bin/true fi } Stop_Nginx(){ if [ -f $pidfile ];then /application/nginx/sbin/nginx -s stop &>/dev/null action "Nginx is Stopped" /bin/true else echo "Nginx is already Stopped" fi } Reload_Nginx(){ if [ -f $pidfile ];then /application/nginx/sbin/nginx -s reload &>/dev/null action "Nginx is Reloaded" /bin/true else echo "Can't open $pidfile ,no such file or directory" fi } case $1 in start) Start_Nginx RETVAL=$? ;; stop) Stop_Nginx RETVAL=$? ;; restart) Stop_Nginx sleep 3 Start_Nginx RETVAL=$? ;; reload) Reload_Nginx RETVAL=$? ;; *) echo "USAGE: $0 {start|stop|reload|restart}" exit 1 esac exit $RETVAL
編寫完成後測試:
[root@devops01-web-53 scripts]# sh nginx.sh USAGE: nginx.sh {start|stop|reload|restart} [root@devops01-web-53 scripts]# sh nginx.sh start Nginx is Started [ OK ] [root@devops01-web-53 scripts]# ps -ef|grep nginx root 12765 1 0 00:46 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx www 12767 12765 0 00:46 ? 00:00:00 nginx: worker process www 12768 12765 0 00:46 ? 00:00:00 nginx: worker process www 12769 12765 0 00:46 ? 00:00:00 nginx: worker process www 12770 12765 0 00:46 ? 00:00:00 nginx: worker process root 12772 12648 0 00:46 pts/2 00:00:00 grep nginx [root@devops01-web-53 scripts]# sh nginx.sh stop Nginx is Stopped [ OK ] [root@devops01-web-53 scripts]# sh nginx.sh reload Can't open /application/nginx/logs/nginx.pid ,no such file or directory [root@devops01-web-53 scripts]# sh nginx.sh restart Nginx is already Stopped Nginx is Started [ OK ] [root@devops01-web-53 scripts]#
1.2 配置開機自啟動
加入開機自啟動:
[root@devops01-web-53 scripts]# cp nginx.sh /etc/init.d/nginx [root@devops01-web-53 scripts]# chmod +x /etc/init.d/nginx [root@devops01-web-53 scripts]# ll /etc/init.d/nginx -rwxr-xr-x. 1 root root 992 Jul 26 00:48 /etc/init.d/nginx [root@devops01-web-53 scripts]# chkconfig --list nginx service nginx does not support chkconfig [root@devops01-web-53 scripts]#
舉例查看network開機自啟動:
[root@devops01-web-53 scripts]# head /etc/init.d/network #! /bin/bash # # network Bring up/down networking # # chkconfig: 2345 10 90 # description: Activates/Deactivates all network interfaces configured to # start at boot time. # ### BEGIN INIT INFO # Provides: $network [root@devops01-web-53 scripts]# chkconfig --list network network 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@devops01-web-53 scripts]# chkconfig network on [root@devops01-web-53 scripts]# chkconfig --list network network 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@devops01-web-53 scripts]#
然後把這兩行加入/etc/init.d/nginx腳本中
# chkconfig: 2345 10 90
# description: #添加描述服務信息
然後找開機自啟動:開機順序從第K30開始查找,發現K31沒有被占用,那麽nginx開機就使用K31(以此類推)
[root@devops01-web-53 ~]# ll /etc/rc.d/rc3.d/|grep 30
lrwxrwxrwx. 1 root root 17 May 14 02:40 K30postfix -> ../init.d/postfix
[root@devops01-web-53 ~]# ll /etc/rc.d/rc3.d/|grep 31
[root@devops01-web-53 ~]#
關機順序:第60沒有,就用60號
[root@devops01-web-53 ~]# ll /etc/rc.d/rc3.d/|grep 60
[root@devops01-web-53 ~]#
那麽:nginx開機關機順序就設置如下:30為開機順序,60為關機順序
# chkconfig: 2345 30 60
# description: Nginx is a http server or forward
放到/etc/init.d/nginx啟動腳本中第2、3行中
如下所示:
[root@devops01-web-53 scripts]# vim /etc/init.d/nginx #!/bin/bash # chkconfig: 2345 30 60 # description: Nginx is a http server or forward [ -f /etc/init.d/functions ] && . /etc/init.d/functions pidfile=/application/nginx/logs/nginx.pid Start_Nginx(){ if [ -f $pidfile ];then echo "Nginx is running" else /application/nginx/sbin/nginx &>/dev/null action "Nginx is Started" /bin/true fi } Stop_Nginx(){ if [ -f $pidfile ];then /application/nginx/sbin/nginx -s stop &>/dev/null action "Nginx is Stopped" /bin/true else echo "Nginx is already Stopped" fi } Reload_Nginx(){ if [ -f $pidfile ];then /application/nginx/sbin/nginx -s reload &>/dev/null action "Nginx is Reloaded" /bin/true else echo "Can't open $pidfile ,no such file or directory" fi } case $1 in start) Start_Nginx RETVAL=$? ;; stop) Stop_Nginx RETVAL=$? ;; restart) Stop_Nginx sleep 3 Start_Nginx RETVAL=$? ;; reload) Reload_Nginx RETVAL=$? ;; *) echo "USAGE: $0 {start|stop|reload|restart}" exit 1 esac exit $RETVAL
添加開機自啟動:
[root@devops01-web-53 scripts]# chkconfig --add nginx
[root@devops01-web-53 scripts]# chkconfig --list nginx
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@devops01-web-53 scripts]# chkconfig nginx off
[root@devops01-web-53 scripts]# chkconfig --list nginx
nginx 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@devops01-web-53 scripts]# ll /etc/rc.d/rc3.d/|grep nginx
lrwxrwxrwx. 1 root root 15 Jul 26 01:07 K60nginx -> ../init.d/nginx #chkconfig nginx off實質就是創建此文件
[root@devops01-web-53 scripts]# chkconfig nginx on
[root@devops01-web-53 scripts]# ll /etc/rc.d/rc3.d/|grep nginx #chkconfig nginx on實質就是創建此文件
lrwxrwxrwx. 1 root root 15 Jul 26 01:09 S30nginx -> ../init.d/nginx
最後查看已開啟了。
[root@devops01-web-53 scripts]# chkconfig --list nginx
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
1.3 重啟機器,驗證開機自啟動是否生效
最後重啟機器,驗證開機是否自啟動nginx服務。
[root@devops01-web-53 scripts]# reboot
機器已開機,驗證如下:說明開機自啟動配置成功。
[root@devops01-web-53 ~]# ps -ef|grep nginx
root 1202 1 0 01:12 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx
www 1204 1202 0 01:12 ? 00:00:00 nginx: worker process
www 1205 1202 0 01:12 ? 00:00:00 nginx: worker process
www 1206 1202 0 01:12 ? 00:00:00 nginx: worker process
www 1207 1202 0 01:12 ? 00:00:00 nginx: worker process
root 1292 1276 0 01:15 pts/0 00:00:00 grep nginx
[root@devops01-web-53 ~]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 1202 root 6u IPv4 12016 0t0 TCP *:http (LISTEN)
nginx 1204 www 6u IPv4 12016 0t0 TCP *:http (LISTEN)
nginx 1205 www 6u IPv4 12016 0t0 TCP *:http (LISTEN)
nginx 1206 www 6u IPv4 12016 0t0 TCP *:http (LISTEN)
nginx 1207 www 6u IPv4 12016 0t0 TCP *:http (LISTEN)
[root@devops01-web-53 ~]# netstat -lntup|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1202/nginx
[root@devops01-web-53 ~]#
nginx啟動腳本編寫及設置開機自啟動