Shell脚本 - nginx启动脚本

OS:CentOS/Redhat 系列

并在 Centos 6.7 和 Centos 7.2 上测试正常

#!/bin/bash
#
# auth: daxin
# time: 2018/07/10
#
# nginx    start nginx web server
# 
# chkconfig: - 10 90
# description: Start, stops and reload nginx web server
#
# config: /usr/local/nginx/conf/nginx.conf
# config: /usr/local/nginx/conf/vhosts
#
### BEGIN INIT INFO
# Provides: nginx
# Required-Start:
# Required-Stop:
# Default-Start: 3
# Default-Stop: 
# Short-Description: start and stop nginx web server
# Description: Start, stop and reload nginx web server
### END INIT INFO

# Source function library
. /etc/init.d/functions

NGINX_DIR="/usr/local/nginx"
NGINX="${NGINX_DIR}/sbin/nginx"
NGINX_CONF="${NGINX_DIR}/conf/nginx.conf"
PROG=$(basename $NGINX)
LOCK_FILE="${NGINX_DIR}/${PROG}.lock"

# check Command exists
if [ ! -x ${NGINX} ]; then
    echo -n $"${NGINX} does not exists."; warning; echo
    exit 5
fi

function start() {
    [ -f $NGINX_CONF ] || exit 6 
    $NGINX -c $NGINX_CONF 
    retval=$?
    if [ $retval -eq 0 ]; then
        touch $LOCK_FILE
        action "Starting $PROG" /bin/true
    else
        action "Starting $PROG" /bin/false
    fi
    return $retval
}

function stop() {
    pkill $PROG
    retval=$?
    if [ $retval -eq 0 ]; then
        rm -f $LOCK_FILE
        action "Stop $PROG" /bin/true
    else
        action "Stop $PROG" /bin/false   
    fi
    return $retval
}

function restart() {
    configtest_q || return 6
    stop
    start
}

function reload() {
    configtest_q || return 6
    nginx_main_process=$(ps aux | grep $NGINX | grep -v grep | awk '{print $2}')
    kill -HUP $nginx_main_process
    retval=$?
    if [ $retval -eq 0 ]; then
        action "Reload $PROG" /bin/true
    else
        action "Reload $PROG" /bin/false
    fi
    return $retval
}

function configtest_q() {
    $NGINX -t -c $NGINX_CONF
}

function status() {
    nginx_main_process=$(ps aux | grep $NGINX | grep -v grep | awk '{print $2}')
    if [ -z $nginx_main_process ]; then
        action "$PROG is not running" /bin/false
    else
        action "$PROG is runing" /bin/true
    fi
}


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

  

posted @ 2018-07-10 17:05  SpeicalLife  阅读(4268)  评论(0编辑  收藏  举报