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 #對服務進行操作


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章