nginx-启动脚本-添加到服务

vim /etc/init.d/nginx

#!/bin/bash

# chkconfig: 2345 99 20

# description: Nginx Service Control Script

PROG="/usr/local/nginx/sbin/nginx"

PIDF="/usr/local/nginx/logs/nginx.pid"

case "$1" in

start)

netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null

if [ $? -eq 0 ]

then

echo "Nginx service already running."

else

     $PROG -t &> /dev/null

if [ $? -eq 0 ] ; then

       $PROG

echo "Nginx service start success."

else

     $PROG -t

fi

fi

   ;;

stop)

netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null

if [ $? -eq 0 ]

then

kill -s QUIT $(cat $PIDF)

echo "Nginx service stop success."

else

echo "Nginx service already stop"

fi

   ;;

restart)

    $0 stop

    $0 start

    ;;

status)

netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null

if [ $? -eq 0 ]

then

echo "Nginx service is running."

else

echo "Nginx is stop."

fi

  ;;

reload)

netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null

if [ $? -eq 0 ]

then

    $PROG -t &> /dev/null

if [ $? -eq 0 ] ; then

kill -s HUP $(cat $PIDF)

echo "reload Nginx config success."

else

      $PROG -t

fi

else

echo "Nginx service is not run."

fi

    ;;

  *)

echo "Usage: $0 {start|stop|restart|reload}"

exit 1

esac

然后给脚本添加权限

chmod +x /etc/init.d/nginx  #给脚本添加可执行权限

chkconfig --add nginx    #nginx添加为系统服务

 

chkconfig nginx on      #nginx添加开机自启动

 

 chkconfig --list nginx    #查看nginx开机启动项

 

posted @ 2023-07-19 09:58  ღ᭄遇见你²⁰²²  阅读(62)  评论(0)    收藏  举报