环境: Centos 6.8
如果机器是 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 ~]#
来源: http://www.bubuko.com/infodetail-2700361.html