Linux Chkconfig Service Registration (Service registration script description)

Source: Internet
Author: User
Tags haproxy

Some services on Linux are restarted and started with the system boot, and can be registered as a service via Chkconfig as Windows. You need to have the corresponding startup script in the/etc/init.d/directory of Linux when registering chkconfig.

I. Registration Service Script Description

1. The name of the script under the/etc/init.d/directory is the service name used when the service is registered.

2, in the service script generally includes start/stop/restart/status/condrestart/reload several operations

Start: Start the service

STOP: Stop service

Status: View service status

Condrestart:: Similar to restart, but does not perform a reboot until the service is present

Restart: Restart the service and refer directly to the service when the service process does not exist

Reload: Do not reboot, re-read the configuration file of the service load

3. Standard service Registration script template (using Keepalived's registration script):

#!/bin/sh
#
# Startup script for the keepalived daemon
#
# processname:keepalived
# Pidfile:/var/run/ Keepalived.pid
# config:/etc/keepalived/keepalived.conf
# chkconfig:-#此处必须有, Is the configuration of the Chkconfig service registered to the Linux boot 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" #服务的进程文件名, process number file name Keepalived.pid

start () {
    echo-n $ "Starting $prog:"
    daemon keepalived ${keepalived_ OPTIONS}
    retval=$?
    echo
    [$RETVAL-eq 0] && touch/var/lock/subsys/$prog #锁定进程, Later find out whether the process exists through this file
}

stop () {
    echo-n $ "Stopping $prog:"
    killproc keepalived #默认到/var/lock/ subsys/,/var/run directory to find the corresponding file and PID and then 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 "$" 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: $ {Start|stop|reload|restart|condrestart|status}"
Exit 1
Esac

Exit $RETVAL


4. Non-standard registration script (using Haproxy registration script)

#!/bin/sh
#
# Startup script for the Haproxy daemon
#
# Processname:haproxy
# Description:start and Stop Haproxy
# Chkconfig:-#同上必写
. /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 "$" in
Start
Start
Error=$?
;;
Stop
Stop
Error=$?
;;
Restart
Stop
Start
Error=$?
;;
Status
Status-p/usr/local/logs/haproxy.pid #检查服务状态, judging by process number file
Error=$?
;;
*)
echo "Usage: $ {Start|stop|restart}"
Esac
Exit $error

5, register script writing experience:

           chkconfig service is System automatic to/etc/ The INIT.D directory locates the corresponding file according to the service name being passed, and then the file performs the action command (start, stop) that is passed. Service scripts are based on the startup script of the service, the view process to start the service and view the status of the service, and execute the KILL command to stop the service.

The standard service service starts by logging the boot process number to the/var/run directory and locking the service in the/var/subsys/directory. Therefore, the registration service can be written according to Keepalived's script. When the service process number file directory is non-standard, at this time according to keepavived script write command will be error, you need to write a script according to the specific configuration of the service to find the process number to stop, or execute the Service startup script execution start.

Second, chkconfig Command common operation:

Chkconfig--add Service Name # Registration Service

Chkconfig--del Service Name # Delete Service

Chkconfig--list Service Name # View the level of service startup

Chkconfig--level 2, 3 service name On|off turn on or off at a certain level of service

Service Name Start|stop|restart #对服务进行操作


Linux Chkconfig Service Registration (Service registration script description)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.