nginx启动脚本

Posted JahanGu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx启动脚本相关的知识,希望对你有一定的参考价值。

#!/bin/bash

CURRDIR=`dirname "$0"`
BASEDIR=`cd "$CURRDIR"; pwd`

NAME="nginx"
CMD=/usr/local/nginx/sbin/nginx

if [ "$1" = "-d" ]; then
    shift
    EXECUTEDIR=$1/
    shift
else
    EXECUTEDIR=$BASEDIR/
fi

if [ ! -d "$EXECUTEDIR" ]; then
    echo "ERROR: $EXECUTEDIR is not a dir"
    exit
fi

if [ ! -d "$EXECUTEDIR"/conf ]; then
    echo "ERROR: could not find $EXECUTEDIR/conf/"
    exit
fi

if [ ! -d "$EXECUTEDIR"/logs ]; then
    mkdir "$EXECUTEDIR"/logs
fi

cd "$EXECUTEDIR"
PID_FILE="$EXECUTEDIR"/logs/nginx.pd

check_pid() {
    RETVAL=1
    if [ -f $PID_FILE ]; then
        PID=`cat $PID_FILE`
        ls /proc/$PID &> /dev/null
        if [ $? -eq 0 ]; then
            RETVAL=0
        fi
    fi
}

check_running() {
    PID=0
    RETVAL=0
    check_pid
    if [ $RETVAL -eq 0 ]; then
        echo "$CMD is running as $PID, we‘ll do nothing"
        exit
    fi
}

start() {
    check_running
    #echo "$CMD -c $EXECUTEDIR/conf/nginx.conf -p $EXECUTEDIR"
    "$CMD" -c "$EXECUTEDIR/conf/nginx.conf" -p "$EXECUTEDIR"
}

stop() {
    "$CMD" -c "$EXECUTEDIR/conf/nginx.conf" -p "$EXECUTEDIR" -s stop
}

status() {
    check_pid
    if [ $RETVAL -eq 0 ]; then
        echo "nginx is running as $PID ..."
    else
        echo "nginx is not running"
    fi
}

reload() {
    "$CMD" -c "$EXECUTEDIR/conf/nginx.conf" -p "$EXECUTEDIR" -t
    if [ $? -ne 0 ]; then
        echo "test nginx conf fail. please check it first, we won‘t reload it"
        exit 1
    fi
    "$CMD" -c "$EXECUTEDIR/conf/nginx.conf" -p "$EXECUTEDIR" -s reload
}

RETVAL=0
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        status
        ;;
    reload)
        reload
        ;;
    *)
    echo "Usage: $0 {start|stop|restart|status|reload}"
    RETVAL=1
esac
exit $RETVAL

 

以上是关于nginx启动脚本的主要内容,如果未能解决你的问题,请参考以下文章

sh Unbounce脚本片段,用于在零停机时间内重新启动HAProxy

Shell脚本切割日志

nginx启动脚本

nginx配置快捷启动service

Nginx 启动脚本/重启脚本

nginx编译安装服务启动脚本在哪