nginx启动脚本编写及设置开机自启动
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx启动脚本编写及设置开机自启动相关的知识,希望对你有一定的参考价值。
环境:Centos 6.8
如果机器是Centos 7的,此脚本和设置开机自启动方法不适用。
首先确保nginx配置文件中:有pid目录
pid logs/nginx.pid;
1.1 编写nginx启动脚本
[[email protected] ~]# cd /server/scripts [[email protected] 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
编写完成后测试:
[[email protected] scripts]# sh nginx.sh USAGE: nginx.sh {start|stop|reload|restart} [[email protected] scripts]# sh nginx.sh start Nginx is Started [ OK ] [[email protected] 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 [[email protected] scripts]# sh nginx.sh stop Nginx is Stopped [ OK ] [[email protected] scripts]# sh nginx.sh reload Can't open /application/nginx/logs/nginx.pid ,no such file or directory [[email protected] scripts]# sh nginx.sh restart Nginx is already Stopped Nginx is Started [ OK ] [[email protected] scripts]#
1.2 配置开机自启动
加入开机自启动:
[[email protected] scripts]# cp nginx.sh /etc/init.d/nginx [[email protected] scripts]# chmod +x /etc/init.d/nginx [[email protected] scripts]# ll /etc/init.d/nginx -rwxr-xr-x. 1 root root 992 Jul 26 00:48 /etc/init.d/nginx [[email protected] scripts]# chkconfig --list nginx service nginx does not support chkconfig [[email protected] scripts]#
举例查看network开机自启动:
[[email protected] 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 [[email protected] scripts]# chkconfig --list network network 0:off 1:off 2:on 3:on 4:on 5:on 6:off [[email protected] scripts]# chkconfig network on [[email protected] scripts]# chkconfig --list network network 0:off 1:off 2:on 3:on 4:on 5:on 6:off [[email protected] scripts]#
然后把这两行加入/etc/init.d/nginx脚本中
# chkconfig: 2345 10 90
# description: #添加描述服务信息
然后找开机自启动:开机顺序从第K30开始查找,发现K31没有被占用,那么nginx开机就使用K31(以此类推)
[[email protected] ~]# ll /etc/rc.d/rc3.d/|grep 30
lrwxrwxrwx. 1 root root 17 May 14 02:40 K30postfix -> ../init.d/postfix
[[email protected] ~]# ll /etc/rc.d/rc3.d/|grep 31
[[email protected] ~]#
关机顺序:第60没有,就用60号
[[email protected] ~]# ll /etc/rc.d/rc3.d/|grep 60
[[email protected] ~]#
那么:nginx开机关机顺序就设置如下:30为开机顺序,60为关机顺序
# chkconfig: 2345 30 60
# description: Nginx is a http server or forward
放到/etc/init.d/nginx启动脚本中第2、3行中
如下所示:
[[email protected] 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
添加开机自启动:
[[email protected] scripts]# chkconfig --add nginx
[[email protected] scripts]# chkconfig --list nginx
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[[email protected] scripts]# chkconfig nginx off
[[email protected] scripts]# chkconfig --list nginx
nginx 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[[email protected] 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实质就是创建此文件
[[email protected] scripts]# chkconfig nginx on
[[email protected] 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
最后查看已开启了。
[[email protected] scripts]# chkconfig --list nginx
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
1.3 重启机器,验证开机自启动是否生效
最后重启机器,验证开机是否自启动nginx服务。
[[email protected] scripts]# reboot
机器已开机,验证如下:说明开机自启动配置成功。
[[email protected] ~]# 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
[[email protected] ~]# 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)
[[email protected] ~]# netstat -lntup|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1202/nginx
[[email protected] ~]#
以上是关于nginx启动脚本编写及设置开机自启动的主要内容,如果未能解决你的问题,请参考以下文章
Centos 7关于rc.local脚本命令开机不执行及指定用户启动的解决方法