Write nginx service control script under Centos6 (with annotation analysis) to realize chkconfig tool management service

Write nginx service control script under Centos6 and manage it with chkconfig

1. Scripting

  • Make sure that nginx has been installed on the system. For related installation documents, please refer to previous articles:

    Linux software management: configure epel warehouse to install nginx/source code to install nginx/

  • Script writing, we directly use the script provided by the official link below, and make script comments below

    Red Hat NGINX Init Script

  • We save the script file under /etc/init.d/nginx

  • Note: If you manually customize the installation of nginx service, you need to modify

    nginx="/usr/sbin/nginx" is modified to the path of nginx program execution

    NGINX_CONF_FILE="/etc/nginx/nginx.conf" is modified to the path of the configuration file

    File executable permission 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
  • Perform nginx service management (ie execute script files)
$ /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. Use chkconfig for management

  • Requirement analysis: If we want to realize nginx start-up automatically, then we have to write related commands into the rc.local file, there are other advanced services, the operations involved are more cumbersome, the chkconfig service came into being, simplified These processes.
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服务开机自启动

Guess you like

Origin blog.csdn.net/weixin_31789689/article/details/108231054