nginx 设置开机启动 和 启动,停止命令

nginx 设置开机启动 和 启动,停止命令

  1. chkconfig 设置开机启动, chkconfig --add nginxd
  2. nginx shell 放在 /etc/init.d 下

nginxd 脚本内容

#!/bin/bash
# chkconfig: 2345 40 98
# desc: stop start restart nginx server

#定义变量
path=/usr/local/nginx/sbin
pid=/usr/local/nginx/logs/nginx.pid
RESULT=0

#加载函数库
. /etc/init.d/functions

start() {
  if  [ ! -f $pid ]; then
    $path/nginx
    RESULT=$?
    if [ $RESULT -eq 0 ]; then 
        action "nginx is started" /bin/true
        return $RESULT
    else
        action "nginx is started" /bin/false
        return $RESULT
    fi
  else
    echo "nginx is runing"
    return 0
  fi
}

stop() {
  if [ -f $pid ]; then
    $path/nginx -s stop
    RESULT=$?
    if [ $RESULT -eq 0 ]; then
       action "nginx is stoped" /bin/true
       return $RESULT
    else
       action "nginx is stoped" /bin/false
       return $RESULT
    fi   
  else
    ehco "nginx is no runing"
    return $RESULT
  fi
}


case "$1" in
        start)
                start
                RESULT=$?
                ;;
        stop)
                stop
                RESULT=$?
                ;;
        restart)
                stop
                sleep 1
                start
                RESULT=$?
                ;;
        *)
                echo $"Usage: $0 {start|stop|restart}"
                exit 1
esac
exit $RESULT

demo

./nginxd [start|stop|restart]

知识点

  1. chkconfig
  2. case / if / [ ]
  3. 加载系统函数和系统函数操作
tags: Linux,Nginx,chkconfig
chenshuai 10个月前
可以可以