码迷,mamicode.com
首页 > 系统相关 > 详细

Shell脚本 - nginx启动脚本

时间:2018-07-10 17:53:51      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:pre   efault   nginx   bash   basename   print   ||   dax   esc   

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
  

  

Shell脚本 - nginx启动脚本

标签:pre   efault   nginx   bash   basename   print   ||   dax   esc   

原文地址:https://www.cnblogs.com/dachenzi/p/9290132.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!