CentOS的System V init啟動指令碼

安全劍客發表於2020-04-22
導讀 系統本身自帶了說明,在/usr/share/doc/initscripts-(*)/sysvinitfiles,內容如下:所有System V init 都命名為/etc/rc.d/init.d/<servicename>,其中</servicename><servicename>是服務的名稱。必須沒有“.init”字尾。

CentOS的System V init啟動指令碼CentOS的System V init啟動指令碼

示例 :

#!/bin/bash
#
# /etc/rc.d/init.d/<servicename>
#
# <description of the *service*>
# <any general comments about this init script>
#
# <tags -- see below for tag definitions. *Every line* from the top
# of the file to the end of the tags section must begin with a #
# character. After the tags section, there should be a blank line.
# This keeps normal comments in the rest of the file from being
# mistaken for tags, should they happen to fit the pattern.>
# Source function library.
. /etc/rc.d/init.d/functions
<define any local shell functions used by the code that follows>
case "$1" in
    start)
        echo -n "Starting <servicename> services: "
        <start daemons, perhaps with the daemon function>
        touch /var/lock/subsys/<servicename>
    ;;
    stop)
        echo -n "Shutting down <servicename> services: "
        <stop daemons, perhaps with the killproc function>
        rm -f /var/lock/subsys/<servicename>
    ;;
    status)
        <report the status of the daemons in free-form format,
        perhaps with the status function>
    ;;
    restart)
        <restart the daemons, normally with $0 stop; $0 start>
    ;;
    reload)
        <cause the service configuration to be reread, either with
        kill -HUP or by restarting the daemons, possibly with
        $0 stop; $0 start>
    ;;
    probe)
        <optional. If it exists, then it should determine whether
        or not the service needs to be restarted or reloaded (or
        whatever) in order to activate any changes in the configuration
        scripts. It should print out a list of commands to give to
        $0; see the description under the probe tag below.>
    ;;
    *)
        echo "Usage: <servicename> {start|stop|status|reload|restart[|probe]"
        exit 1
    ;;
esac

注意:重啟和過載功能可以(通常)組合成一個測試,

vis:
restart|reload)

不禁止您新增其他 ; 列出您打算以互動方式使用到使用訊息的所有 。

/etc/rc.d/init.d/functions函式
daemon [+/-nicelevel] program [arguments] [&]

如果守護程式尚未執行,則啟動該守護程式。還有其他一些有用的東西,例如,如果守護程式意外終止,則保留守護程式。

killproc program [signal]

向程式傳送訊號; 預設情況下,它傳送一個SIGTERM,如果程式沒有死,它會在幾秒鐘後傳送一個SIGKILL。

如果找到pid檔案,它還會嘗試刪除它。

pidofproc program

試圖找到一個程式的pid; 檢查可能的pidfiles,使用pidof程式,甚至使用ps。主要用於此檔案中的其他函式,但也可用於指令碼。

status program

列印狀態資訊。假設程式名稱與servicename相同。

Tags.
# chkconfig: <startlevellist> <startpriority> <endpriority>

必須。是預設情況下應啟動服務的級別列表。和是優先順序編號。例如:

# chkconfig:2345 20 80有關詳細資訊,請閱讀“man chkconfig”。

除非有一個非常好的,顯性相反的原因,<endpriority>應該等於 100 - <startpriority>

# description: <multi-line description of service>

必須。幾行描述,繼續使用'\'字元。以下行中的初始註釋和後續空格將被忽略。

# description[ln]: <multi-line description of service in the language \ # ln, whatever that is>

可選。應將描述翻譯成指定的語言。

# processname:

可選,允許多個條目。對於指令碼啟動的每個程式名稱,應該有一個程式名稱條目。例如,samba服務啟動兩個守護程式:

#processname:smdb 
  #processname:nmdb
# config:

可選,允許多個條目。對於守護程式使用的每個靜態配置檔案,請使用單個條目。例如:

# config: /etc/httpd/conf/httpd.conf
  # config: /etc/httpd/conf/srm.conf

可選)如果伺服器將自動重新載入配置檔案(如果已更改),則可以在行中附加“autoreload”一詞:

# config: /etc/foobar.conf autoreload

#pidfile:

可選,允許多個條目。使用就像配置條目一樣,除了它指向pidfiles。假設pidfiles僅在程式建立時更新,而不是更晚。該檔案的第一行應該是PID的ASCII表示; 終止換行符是可選的。不檢查除第一行以外的任何行。

#project: true

可選,使用IN PLACE的processname,config和pidfile。如果存在,則可以透過執行以下命令來實現正確的重新載入 - 如果必要的迴圈:

command = $(/ etc / rd.d / init.d / SCRIPT probe)
[ -  n“$ command”] && /etc/rc.d/init.d/SCRIPT $ command

其中SCRIPT是服務的sysv init指令碼的名稱。

作為示例,需要執行復雜處理的指令碼可以返回“run /var/tmp/<servicename.probe.$$”並實現“run”命令,該命令將執行命名指令碼然後將其刪除。

請注意,如果不需要執行任何操作使服務與其配置檔案同步,則probe命令應該只是“exit 0”。

需要注意以下幾點:

1、# chkconfig和# description不能少,必須寫。

2、chkconfig的<startpriority> <endpriority>為啟動優先順序,在man中查詢不到,一般end...不用理解,直接100-start...即可。start為開始的順序,一般系統從小執行到大,數值任意,這個對於依賴啟動有很大的幫助,比如控制先啟動某個服務,再啟動某個服務。以下是查詢設定後的命令:

# 查詢啟動級別
chkconfig --list <servicename>
# 查詢啟動順序
grep chkconfig /etc/rc.d/init.d/<servicenaem>

原文來自: 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69955379/viewspace-2687495/,如需轉載,請註明出處,否則將追究法律責任。

相關文章