CentOS的System V init啟動腳本

CentOS系統本身自帶了說明,在/usr/share/doc/initscripts-(*)/sysvinitfiles,內容如下:所有System V init腳本都命名為/etc/rc.d/init.d/<servicename>,其中/<servicename><servicename>是服務的名稱。必須沒有“.init”後綴。/<servicename>

CentOS的System V init啟動腳本

示例腳本:

<code>#!/bin/bash
#
# /etc/rc.d/init.d/<servicename>
#
# <description>
#
#
# <tags># 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>

case "$1" in
start)
echo -n "Starting <servicename> services: "
<start>
touch /var/lock/subsys/<servicename>
;;
stop)
echo -n "Shutting down <servicename> services: "
<stop>
rm -f /var/lock/subsys/<servicename>
;;
status)
<report> perhaps with the status function>
;;
restart)
<restart>
;;
reload)
<cause> kill -HUP or by restarting the daemons, possibly with
$0 stop; $0 start>
;;
probe)
<optional.> or not the service needs to be restarted or reloaded (or
whatever) in order to activate any changes in the configuration
/> $0; see the description under the probe tag below.>
;;
*)
echo "Usage: <servicename> {start|stop|status|reload|restart[|probe]"

exit 1
;;
esac
/<servicename>/<optional.>/<cause>/<restart>/<report>/<servicename>/<stop>/<servicename>/<servicename>/<start>/<servicename>/<define>/<tags>
/<description>/<servicename>/<code>

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

<code>vis:

restart|reload)/<code>


不禁止您添加其他命令; 列出您打算以交互方式使用到使用消息的所有命令。

<code>/etc/rc.d/init.d/functions函數

daemon [+/-nicelevel] program [arguments] [&]/<code>

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

<code>killproc program [signal]/<code>

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

如果找到pid文件,它還會嘗試刪除它。

<code>pidofproc program/<code>

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

<code>status program/<code>

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

<code>Tags.

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

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

<code># chkconfig:2345 20 80有關詳細信息,請閱讀“man chkconfig”。/<code>

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

<code># description: <multi-line>/<code>

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

<code># description[ln]: <multi-line>/<code>

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

<code># processname:/<code>

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

<code>#processname:smdb 
  #processname:nmdb

# config:/<code>

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

<code># config: /etc/httpd/conf/httpd.conf
  # config: /etc/httpd/conf/srm.conf/<code>

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

<code># config: /etc/foobar.conf autoreload/<code>

#pidfile:

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

<code>#project: true/<code>

可選,使用IN PLACE的processname,config和pidfile。如果存在,則可以通過運行以下命令來實現正確的重新加載 - 如果必要的循環:

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

其中SCRIPT是服務的sysv init腳本的名稱。

作為示例,需要執行復雜處理的腳本可以返回“run /var/tmp/<servicename.probe.>

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

需要注意以下幾點:

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

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

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


分享到:


相關文章: