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

 

posted @ 2017-10-27 16:49  JahanGu  阅读(374)  评论(0编辑  收藏  举报