Centos6下编写nginx服务控制脚本(附带注释解析),实现chkconfig工具管理服务

Centos6下编写nginx服务控制脚本,并用chkconfig管理

1. 脚本编写

  • 确定系统已经安装了nginx,相关安装文档请查看往期文章:

    linux软件管理:配置epel仓库安装nginx/源码安装nginx/

  • 脚本编写,我们直接采用下述官方连接提供的脚本,并在下面做脚本注释

    Red Hat NGINX Init Script

  • 我们将该脚本文件保存到/etc/init.d/nginx下

  • 注意:若是手动自定义安装nginx服务,需要修改

    nginx="/usr/sbin/nginx" 修改成nginx程序执行的路径

    NGINX_CONF_FILE="/etc/nginx/nginx.conf" 修改为配置文件的路径

    文件可执行权限 chmod a+x nginx

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  NGINX is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions								#函数库地址

# Source networking configuration.
. /etc/sysconfig/network									#网络配置文件地址

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0							 #检查网络是否启动

nginx="/usr/sbin/nginx"										#设置nginx默认路径
prog=$(basename $nginx)										#取 nginx 字串

NGINX_CONF_FILE="/etc/nginx/nginx.conf"						  #nginx配置文件路径

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx		   #启动nignx服务

lockfile=/var/lock/subsys/nginx

make_dirs() {											#创建服务运行所需要的目录
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`         		#取出路径
   if [ -n "$user" ]; then								 #字符串 非空,返回0,为true
      if [ -z "`grep $user /etc/passwd`" ]; then		   #字符串指代的用户名不存在
         useradd -M -s /bin/nologin $user				  #创建用户
      fi
      options=`$nginx -V 2>&1 | grep 'configure arguments:'` #记录选项
      for opt in $options; do
          if [ `echo $opt | grep '.*-temp-path'` ]; then
              value=`echo $opt | cut -d "=" -f 2`          #按 = 分割 取第二段
              if [ ! -d "$value" ]; then
                  # echo "creating" $value
                  mkdir -p $value && chown -R $user $value   #创建文件夹并修改所有者
              fi
          fi
       done
    fi
}

start() {  #启动服务
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {   #停止服务
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {  #重启服务
    configtest || return $?
    stop     #关闭服务
    sleep 1  #睡觉一秒
    start    #启动服务
}

reload() {   #重新加载进程?
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $prog -HUP  
    retval=$?
    echo
}

force_reload() { #强制重启
    restart
}

configtest() {  #查看你配置文件
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {   #服务状态
    status $prog
}

rh_status_q() {  #用处:状态码返回值使用
    rh_status >/dev/null 2>&1
}

case "$1" in       #命令参数判断
    start)
        rh_status_q && exit 0 #服务运行中就不需要启动
        $1
        ;;
    stop)
        rh_status_q || exit 0 #服务在运行就关闭服务,服务本来就不存在那就退出
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7  
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac
  • 进行nginx服务管理(即执行脚本文件)
$ /etc/init.d/nginx start
$ /etc/init.d/nginx stop
$ /etc/init.d/nginx reload
$ /etc/init.d/nginx force-reload
$ /etc/init.d/nginx status
$ /etc/init.d/nginx 
...

2. 使用chkconfig进行管理

  • 需求分析:我们假如要实现nginx开机自启动,那么我们就得将相关命令写入到rc.local文件里,还有其他的高级服务,涉及到的操作比较繁琐,chkconfig服务就应运而生,简化了这些过程。
chkconfig --add /etc/init.d/nginx   #将chkconifg加入管理列表
$ service nginx start			   #我们就可以通过service命令执行nginx服务
$ service nginx stop
$ service nginx reload
$ service nginx status
...
chkconfig --level 3 nginx on 		#指的是再多用户模式下实现nginx服务开机自启动
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值