#!/bin/bash
# chkconfig: 2345 32 62 
# description: nginx is a http server

[ -f /etc/init.d/functions ] && . /etc/init.d/functions
pidfile=/usr/local/nginx/logs/nginx.pid
nginx=/usr/local/nginx/sbin/nginx

usage(){ 
 RETVAL=$?
 if [ $RETVAL -eq 0 ];then
 action "nginx is $1" /bin/true
 else
 action "nginx is $1" /bin/false
 fi
}


Start_nginx(){
 if [ -f $pidfile ]; then
 echo "nginx is runing"
 else
 $nginx start
 RETVAL=$?
 usage STARTED
 fi
 return $RETVAL
}

Stop_nginx(){
 if [ -f $pidfile ]; then
 $nginx -s stop
 usage STOPED
 else
 ection "nginx is spoped" /bin/false
 fi
 return $RETVAL
}


Reload_nginx(){
 if [ -f $pidfile ]; then
 $nginx -s reload
 usage RELOAD
 else
 action "nginx is stop, please start nginx!" /bin/false
 fi
 return $RETVAL
}

case "$1" in
 start)
 Start_nginx
 RETVAL=$?
 ;;
 stop)
 Stop_nginx
 RETVAL=$?
 ;;
 restart)
 Stop_nginx
 sleep 2
 Start_nginx
 RETVAL=$?
 ;;
 reload)
 Reload_nginx
 RETVAL=$?
 ;;
 *)
 echo "usage:$0 {start|stop|reload|restart}"
 exit 1
esac

exit $RETVAL



思路

centos6开机作为启动项时默认会传值start|stop|restart|值

需要注意nginx.pid的文件,随着启动进程号的出现而出现

首先把写好的脚本cp nginx.sh /etc/init.d/nginx

chmod +x /etc/init.d/nginx

脚本开始加上启动顺序具体还要了解下基础

# chkconfig: 2345 10 90
# description: nginx is a http server

需要了解基础contos系统启动

nit.d ll /etc/rc.d/rc3.d |grep 31

init.d ll /etc/rc.d/rc3.d |grep 32

➜  init.d ll /etc/rc.d/rc3.d |grep 61

➜  init.d ll /etc/rc.d/rc3.d |grep 62

然后把脚本启动修改为

# chkconfig: 2345 31 62
# description: nginx is a http server

注意这是centos6的使用方法和7 有不同之处

➜ init.d chkconfig --add nginx
➜ init.d chkconfig --list nginx

chkconfig nginx on/off

自启动的机制就是 server 服务 start

所以加入自启动后系统会自动给脚本传start参数


Last modification:July 22, 2019
如果觉得我的文章对你有用,请随意赞赏