簡體   English   中英

了解 Bash init 腳本啟動/停止/重啟/

[英]Understand Bash init script start/stop/restart/

我們可以在 /etc/rc.d/init.d/ 文件夾下提供很多服務(bash 腳本)。 它們看起來都像這樣:

case "$1" in 
start)   echo "start" ;;
stop)    echo "stop" ;;
restart) echo "restart" ;;
esac

我只是不明白我們啟動我的計算機,內核如何調用這些啟動腳本並傳入參數“start” 或者當服務終止時,誰調用腳本並傳入參數“restart”

誰可以給我解釋一下這個?

提前致謝。

這取決於您的發行版/版本/配置選擇。 對於帶有 System V 風格啟動文件的 Debian,請查看http://www.debian.org/doc/debian-policy/ch-opersys.html#s-sysvinit

粗略地說,每個運行級別都有一個目錄,例如 /etc/rc2.d 用於級別 2,包含指向 /etc/init.d 中常規文件(腳本)的符號鏈接

在某個時間點,當進入第 2 級時,會運行以下腳本循環

for s in /etc/rc2.d/S* 
do
   $s start
done

按字母順序開始執行名稱以 S 開頭的所有鏈接。 實際上 S 后面是兩位數字,指定執行順序。

離開運行級別時,K* 文件的想法相同。

現在回到你的問題:這是 /etc/inittab 文件中一些行的工作

# The default runlevel.
id:2:initdefault:

....
# /etc/init.d executes the S and K scripts upon change
# of runlevel.
#
# Runlevel 0 is halt.
# Runlevel 1 is single-user.
# Runlevels 2-5 are multi-user.
# Runlevel 6 is reboot.

l0:0:wait:/etc/init.d/rc 0
l1:1:wait:/etc/init.d/rc 1
l2:2:wait:/etc/init.d/rc 2
...

init shell 腳本的實際位置在 /etc/init.d 下。 這些腳本符號鏈接到 rc 目錄,如 /etc/rc0.d、/etc/rc1.d、/etc/rc2.d。 然后,在每個 rcn.d 目錄中,我們的文件名以 K 或 S 開頭,后跟兩位數字。 這些是指向實際 init shell 腳本的符號鏈接文件,其中 K 表示終止(即停止),而“S”表示啟動。

例如:
S19postgresql
S20clamav-freshclam
S50saned
S70pppd-dns
S99點播

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM