将nginx交给service管理

#!/bin/bash
# chkconfig: 2345 99 99
prot=80
nginx=/usr/local/nginx/sbin/nginx
check(){
! $nginx -tq && echo "致命错误:配置文件错误" && exit
}
start(){
check
netstat -tln |grep -q ":80\>" && echo "$prot端口被占用" && exit
$nginx
}
stop_reload(){
check
! netstat -tlnp |grep -q "nginx" && echo "nginx 未运行"
$nginx -s $1 &> /dev/null
}
case "$1" in
start)
        start ;;
stop)
        stop_reload $1 ;;
restart)
        stop_reload stop &> /dev/null
        start ;;
reload)
        stop_reload $1 ;;
*)
        echo "用法:$(basename $0) <start|stop|restart|reload>" ;;
esac

将此脚本放至/etc/init.d/下即可用service启停

将nginx添加到chkconfig管理的开机启动中

  chkconfig --add nginx

  chkconfig nginx on

posted @ 2020-11-02 20:53  心恩惠动  阅读(106)  评论(0编辑  收藏  举报