溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

CentOS中System V init啟動腳本怎么用

發(fā)布時(shí)間:2021-11-08 11:05:29 來源:億速云 閱讀:170 作者:小新 欄目:建站服務(wù)器

這篇文章給大家分享的是有關(guān)CentOS中System V init啟動腳本怎么用的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

CentOS系統(tǒng)本身自帶了說明,在/usr/share/doc/initscripts-(*)/sysvinitfiles,內(nèi)容如下:所有System V init 腳本都命名為/etc/rc.d/init.d/<servicename>,其中</servicename><servicename>是服務(wù)的名稱。必須沒有“.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

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

vis:
restart|reload)

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

/etc/rc.d/init.d/functions函數(shù)
daemon [+/-nicelevel] program [arguments] [&]

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

killproc program [signal]

向程序發(fā)送信號; 默認(rèn)情況下,它發(fā)送一個(gè)SIGTERM,如果進(jìn)程沒有死,它會在幾秒鐘后發(fā)送一個(gè)SIGKILL。

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

pidofproc program

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

status program

打印狀態(tài)信息。假設(shè)程序名稱與servicename相同。

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

必須。是默認(rèn)情況下應(yīng)啟動服務(wù)的級別列表。和是優(yōu)先級編號。例如:

# chkconfig:2345 20 80有關(guān)詳細(xì)信息,請閱讀“man chkconfig”。

除非有一個(gè)非常好的,顯性相反的原因,<endpriority>應(yīng)該等于 100 - <startpriority>

# description: <multi-line description of service>

必須。幾行描述,繼續(xù)使用'\'字符。以下行中的初始注釋和后續(xù)空格將被忽略。

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

可選。應(yīng)將描述翻譯成指定的語言。

# processname:

可選,允許多個(gè)條目。對于腳本啟動的每個(gè)進(jìn)程名稱,應(yīng)該有一個(gè)進(jìn)程名稱條目。例如,samba服務(wù)啟動兩個(gè)守護(hù)進(jìn)程:

#processname:smdb 
  #processname:nmdb
# config:

可選,允許多個(gè)條目。對于守護(hù)程序使用的每個(gè)靜態(tài)配置文件,請使用單個(gè)條目。例如:

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

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

# config: /etc/foobar.conf autoreload

#pidfile:

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

#project: true

可選,使用IN PLACE的processname,config和pidfile。如果存在,則可以通過運(yùn)行以下命令來實(shí)現(xiàn)正確的重新加載 - 如果必要的循環(huán):

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

其中SCRIPT是服務(wù)的sysv init腳本的名稱。

作為示例,需要執(zhí)行復(fù)雜處理的腳本可以返回“run /var/tmp/<servicename.probe.$$”并實(shí)現(xiàn)“run”命令,該命令將執(zhí)行命名腳本然后將其刪除。

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

需要注意以下幾點(diǎn):

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

2、chkconfig的<startpriority> <endpriority>為啟動優(yōu)先級,在man中查詢不到,一般end...不用理解,直接100-start...即可。start為開始的順序,一般系統(tǒng)從小執(zhí)行到大,數(shù)值任意,這個(gè)對于依賴啟動有很大的幫助,比如控制先啟動某個(gè)服務(wù),再啟動某個(gè)服務(wù)。以下是查詢設(shè)置后的命令:

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

感謝各位的閱讀!關(guān)于“CentOS中System V init啟動腳本怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI