共计 1359 个字符,预计需要花费 4 分钟才能阅读完成。
#!/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 nginxchkconfig nginx on/off
自启动的机制就是 server 服务 start
所以加入自启动后系统会自动给脚本传 start 参数