本文使用的实验环境为:centos-6.10 实验服务为nginx服务 服务安装位置为/app/nginx-1.8.1,软连接为/app/nginx
当你编译安装了nginx服务后,想让服务像其他服务一样通过“/etc/init.d/服务”来控制服务的启动、停止。
可以通过一些思路来确定脚本的过程:
- 判断Nginx服务的PID 是否存在,不存在就表示服务未启动。
- 通过脚本传入参数start 或 stop等,通过case语句获取参数进行判断
脚本内容:
#!/bin/bash # chkconfig: 2345 40 98 path=/app/nginx/sbin pid=/app/nginx/logs/nginx.pid RETVAL=0 . /etc/init.d/functions start(){ if [ ! -f $pid ];then $path/nginx RETVAL=$? if [ $RETVAL -eq 0 ];then action "nginx is started" /bin/true return $RETVAL else action "nginx is started" /bin/false return $RETVAL fi else echo "nginx is running" return 0 fi } stop(){ if [ -f $pid ];then $path/nginx -s stop RETVAL=$? if [ $RETVAL -eq 0 ];then action "nginx is stopped" /bin/true return $RETVAL else action "nginx is stopped" /bin/false return $RETVAL fi else echo "nginx is no running" return $RETVAL fi } case "$1" in start) start RETVAL=$? ;; stop) stop RETVAL=$? ;; restart) stop sleep 1 start RETVAL=$? ;; *) echo $"Usage:$0 {start|stop|restart}" exit 1 esca exit RETVAL=$?
如果加入到开机自启:
chkconfig --add nginxd
注意脚本授权:
chmod +x /etc/init.d/nginxd