nginx启停脚本

安装nginx时,源码包中未带官方的启动脚本,也就无法使用service nginxd start这种启动方式,查了下资料自己写了一个:

#!/bin/bash

#@version: 0.0.1
#@author: lifafu
#@file: 
#@time: 2017/02/04 08:00
#@company: 

source /etc/profile            #加载系统环境变量
source $HOME/.bash_profile    #加载用户环境变量
#set -o nounset             #引用未初始化变量时终止执行,也可以set -u
#set -o errexit                #执行任何语句返回非0状态时终止执行,也可以set -e

# chkconfig: - 99 50
# 虽然前面带#号,是注释,但要用chkconfig命令注册开机启动服务器的话,该句必不可少,格式也不能错!
# 3个chkconfig参数的含义:
# x:是指定该脚本在哪个系统启动级别下运行,比如你需要在3,4,5上运行,那么第二位就设置成345,我这里用的是”-”,代表在2,3,4,5上都运行
# y:系统启动时,服务启动顺序,需要注意的是,有的程序依赖与别的程序的话,启动顺序就要注意了,比如A程序的启动依赖于B程序的启动,那么A程序的这个值一定要比B程序大
# z:系统终止时,服务终止顺序

# description: Nginx is a high-performance web and proxy server.
# 该句也必不可少,理由同上,你程序的描述和简介,而非本启动脚本

#设置变量
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
nginx_lock=/var/lock/subsys/nginx

#保存退出状态的变量,初始值为0(在linux一般0表示成功,表示OK,非0表示异常,不OK)
RETYAL=0

# 设置程序名称
prog="nginx"

# Source function library.在当前shell中运行的函数库文件
# 在functions中定义了很多函数,在这里可以调用,系统提供的函数文件,这里面实现了很多函数和环境变量,比如start的时候,红色的字显示OK就是这个文件的功劳
. /etc/rc.d/init.d/functions

# Source network configuration.加载网络配置
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# 定义启动函数
start() {
    # 判断程序是否存在,否则异常退出
    [ -x $nginxd ] || { echo “FATAL: No such programme”;exit 4; }
    
    # 判断配置文件是否存在,否则异常退出
    [ -f $nginx_config ] || { echo “FATAL:Config file does not exist”;exit 6; }
    
    # 判断程序是否运行,否则异常退出
    if [ -e $nginx_pid ];then
        echo "nginx already running...."
        exit 1
    fi
    
    # 判断lock文件是否存在,否则异常退出
    if [ -e $nginx_lock ];then
        echo "nginx lock file does exist...."
        exit 1
    fi
    
    # 显示信息,依赖于. /etc/rc.d/init.d/functions
    echo -n $"Starting $prog:"
    
    # 创建pid文件夹
    dir=$(dirname $nginx_pid)
    [ -d $dir ] || mkdir -p $dir
    
    # 调用functions里的daemon函数来启动nginx,daemon()函数主要用于希望脱离控制台,以守护进程形式在后台运行的程序
    daemon --pidfile $nginx_pid $nginxd -c ${nginx_config}
    
    # 把daemon函数调用的结果保存到RETVAL里
    RETVAL=$?
    echo
    
    # 判断RETVALR值,如果是0执行成功,则生成锁文件,锁文件主要用来判断程序是否运行
    [ $RETVAL = 0 ] && touch $nginx_lock
    
    #终止函数,并返回$RETVAL的值,通常用于函数的结束, 本身这段代码也是个函数,所以我们也要返回,返回RETVAL的值
    return $RETVAL
}


# 定义停止函数
stop() {
    echo -n $"Stoping $prog:"
    
    #killproc也在. /etc/rc.d/init.d/functions里面定义
    killproc -p $nginx_pid $prog
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f $nginx_lock $nginx_pid
}

reload() {
         echo -n $"Reloading $prog:"
         #kill -HUP `cat ${nginx_pid}`
         killproc -p $nginx_pid $prog -HUP
         RETVAL=$?
         echo
}

#See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    reload)
        reload
        ;;
    restart)
        stop
        start
        ;;
    #status在. /etc/rc.d/init.d/functions里有定义
    status)
        status $prog
        RETVAL=$?
        ;;
    #输入其他内容提示以下内容
    *)
        echo $"Usage:$prog{start|stop|restart|reload|status|help}"
        exit 1
esac
exit $RETVAL

保存为nginxd,复制到/etc/init.d/中并赋予执行权限就可以使用service nginxd start等命令了;

如果需要加入开机启动:chkconfig --add nginxd;chkconfig nginxd on;

启停程序也可以使用类似的方式自己编写启动脚本,不过这种方式还是有缺陷,就是必须使用root用户运行这类脚本。

原文地址:https://www.cnblogs.com/leffss/p/7845303.html