編寫CentOS的System V init啓動腳本

系統自己自帶了說明,在/usr/share/doc/initscripts-(*)/sysvinitfiles,內容以下:html

全部System V init腳本都命名爲/etc/rc.d/init.d/<servicename>,其中<servicename>是服務的名稱。必須沒有「.init」後綴。linux

示例腳本:shell

#!/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:centos

restart|reload)bash

不由止您添加其餘命令; 列出您打算以交互方式使用到使用消息的全部命令。服務器

/etc/rc.d/init.d/functions函數app

daemon [+/-nicelevel] program [arguments] [&]函數

    若是守護程序還沒有運行,則啓動該守護程序。還有其餘一些有用的東西,例如,若是守護進程意外終止,則保留守護進程。測試

killproc program [signal]this

    向程序發送信號; 默認狀況下,它發送一個SIGTERM,若是進程沒有死,它會在幾秒鐘後發送一個SIGKILL。

    若是找到pid文件,它還會嘗試刪除它。

pidofproc program

    試圖找到一個程序的pid; 檢查可能的pidfiles,使用pidof程序,甚至使用ps。主要用於此文件中的其餘函數,但也可用於腳本。

status program

    打印狀態信息。假設程序名稱與servicename相同。

Tags.

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

    必須。<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」。 

須要注意如下幾點:

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

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

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

 

 

參考:

https://blog.hazrulnizam.com/create-init-script-centos-6/

https://serverfault.com/questions/176055/how-to-change-linux-services-startup-boot-order

http://www.sensi.org/~alec/unix/redhat/sysvinit.html

相關文章
相關標籤/搜索