Linux chkconfig service registration (service registration script description)

 The restart of some services on linux and the startup with the system startup can be registered as a service through chkconfig like windows. When registering chkconfig, you need to have a corresponding startup script in the /etc/init.d/ directory of linux.

1. Description of the registration service script

1. The script name in the /etc/init.d/ directory is the service name used for service registration.

2. The service script generally includes several operations of start/stop/restart/status/condrestart/reload

         start: start the service

         stop: stop the service

         status: View service status

         condrestart::similar to restart, but restarts only if the service exists

          restart: restart the service, directly mentioning the service when the service process does not exist

          reload: do not restart, re-read and load the configuration file of the service

3. Standard service registration script template (using keepalived registration script):

#!/bin/sh
#
# Startup script for the Keepalived daemon
#
# processname: keepalived
# pidfile: /var/run/keepalived.pid
# config: /etc/keepalived/keepalived.conf
# chkconfig: - 21 79 #here Must have, it is the configuration that the chkconfig service is registered to the linux startup level
# description: Start and stop Keepalived

# Source function library
. /etc/rc.d/init.d/functions #加载脚本使用到的函数例如status、killproc

# Source configuration file (we set KEEPALIVED_OPTIONS there)
. /etc/sysconfig/keepalived #服务的配置文件

RETVAL=0 #状态码

prog="keepalived" #服务的进程文件名,进程号文件名keepalived.pid

start() {
    echo -n $"Starting $prog: "
    daemon keepalived ${KEEPALIVED_OPTIONS}
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog #锁定进程,后面查找进程是否存在通过此文件
}

stop() {
    echo -n $"Stopping $prog: "
    killproc keepalived #默认到/var/lock/subsys/、/var/run目录下查找对应的文件和pid 然后kill
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog #删除进程锁定文件
}

reload() {
    echo -n $"Reloading $prog: "
    killproc keepalived -1 #查找配置文件并重新加载
    RETVAL=$?
    echo
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    reload)
        reload
        ;;
    restart)
        stop
        start
        ;;
    condrestart)
        if [ -f /var/lock/subsys/$prog ]; then
            stop
            start
        fi
        ;;
    status)
        status keepalived
        ;;
    *)
        echo "Usage: $0 {start|stop|reload|restart|condrestart|status}"
        exit 1
esac

exit $RETVAL


4、非标准注册脚本(使用haproxy注册脚本)

#!/bin/sh
#
# Startup script for the Haproxy daemon
#
# processname: haproxy
# description: Start and stop haproxy
# chkconfig: - 21 79 #同上必写
. /etc/rc.d/init.d/functions

basedir=/usr/local/sbin #服务启动脚本目录
confdir=/etc/haproxy #配置文件目录
start() {
 echo "START HAPROXY SERVERS"
 ${basedir}/haproxy -f ${confdir}/haproxy.cfg  #执行启动命令
}
stop() {
 echo "STOP HAPORXY lISTEN"
 kill -TTOU $(cat /usr/local/logs/haproxy.pid) #停止提供服务
 echo "STOP HAPROXY PROCESS"
 kill -usr1 $(cat /usr/local/logs/haproxy.pid) #杀死进程
}
# See how we were called.
case "$1" in
    start)
        start
 error=$?
        ;;
    stop)
        stop
 error=$?
        ;;
    restart)
        stop
        start
 error=$?
        ;;
    status)
 status -p /usr/local/logs/haproxy.pid #检查服务状态,根据进程号文件判断
 error=$?
 ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
esac
exit $error

5、注册脚本编写体会:

           chkconfig服务是系统自动到/etc/init.d目录下根据传递的服务名查找到对应的文件,然后文件执行传递的操作命令(start、stop)。服务脚本是根据服务的启动脚本、查看进程来进行启动服务和查看服务状态,以及执行kill命令来停服务。

          标准的服务服务在启动时会把启动后的进程号记录到/var/run的目录下,在/var/subsys/目录下锁定服务。因此可以根据keepalived的脚本来写注册服务。当服务进程号文件目录是非标准的,此时按照keepavived的脚本写命令会报错,需要根据服务的具体配置来写脚本找到进程号进行停,或执行服务的启动脚本执行start。

二、chkconfig命令常用操作:

         chkconfig --add  服务名#注册服务

         chkconfig --del  服务名#删除服务

         chkconfig  --list 服务名# 查看服务启动的级别

         chkconfig --level 2,3  服务名 on|off开启或关闭在某个级别的服务

         service 服务名 start|stop|restart #对服务进行操作


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325354196&siteId=291194637