Linux的chkconfig服务注册(服务注册脚本说明)

Linux上一些服务的重启以及随系统启动而启动,可以像Windows那样注册为服务通过chkconfig 进行操作。在注册chkconfig时需要在Linux的/etc/init.d/目录下有对应的启动脚本

一、注册服务脚本说明

1、/etc/init.d/目录下的脚本名称就是服务注册时使用的服务名。

2、在服务脚本中一般包括start/stop/restart/status/condrestart/reload几种操作

        start:启动服务

        stop:停止服务

        status:查看服务状态

        condrestart::类似restart,但是只有在服务存在时才会执行重启

          restart:重启服务,在服务进程不存在时直接提到服务

          reload:不进行重启,对服务的配置文件重新读取加载

3、标准服务注册脚本模板(使用keepalived的注册脚本):

#!/bin/sh
#
# Startup script for the Keepalived daemon
#
# processname: keepalived
# pidfile: /var/run/keepalived.pid
# config: /etc/keepalived/keepalived.conf
# chkconfig: – 21 79 #此处必须有,是chkconfig服务注册到linux启动级别的配置
# 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 #对服务进行操作

本文永久更新链接地址:https://www.linuxidc.com/Linux/2018-05/152245.htm

Linux的chkconfig服务注册(服务注册脚本说明)

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

栗子博客 软件 Linux的chkconfig服务注册(服务注册脚本说明) https://www.lizi.tw/soft/9017.html

常见问题
  • 1、杰齐1.7仅适用于PHP5.2 2、需Zend支持 3、尽量使用宝塔面板 4、尽量使用Windows 系统,关关对Linux支持不太友好。
查看详情

相关文章

评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

Linux的chkconfig服务注册(服务注册脚本说明)-海报

分享本文封面