Red Hat 系統服務管理 init

當系統核心 kernel 開機完成後,整個開機程序就交到 init 這支程式身上,當我們 exit 登出時,其實 shell 程式就中止了,這時藉由 init 來將將 shell 重新啟動帶出登入畫面,這個動作就叫 respawn,它會將 /sbin/mingetty 重新 respawn,讓使用者可以再次登入。

init 的設定在 /etc/inittab 中變更,舉例來說,如果要在 serial port 監聽登入動作,可以在 /etc/inittab 加入這一行:

co:23:respawn:/sbin/agetty  -f  /etc/issue.serial  19200  ttyS1

其中 co 是代表 serial port 的連線代號,23 表示只在 runlevel 2 及 runlevel 3 適用,它會 respawn /sbin/agetty 這支程式供使用者登入,/sbin/agetty 的執行參數是 -f /etc/issue.serial 19200,-f /etc/issue.serial 是指登入時用 /etc/issue.serial 的檔案內容來作為登入畫面的資訊、19200 則是 serial port 的連線速度,而 ttyS1 則是 serial port 的裝置代號。

==============================

System V Service

當 init 執行時,會去相對應的執行層級 (runlevel) 去取得服務的腳本,這些腳本是屬於 System V 的服務方式,它是將整個服務的執行程序及啟動方式由一個和服務名稱相同的腳本來管理 (大部份名稱是相同的,但也有例外,如 smb 服務就要啟動 smbd 和 nmbd 兩個腳本,卻沒有 smb 這個腳本),而啟動服務的方式可以直接執行該腳本,或是使用 service 命令,例如要重新啟動網路服務:

#/etc/init.d/network restart (/etc/init.d/ 是個連結,指向 /etc/rc.d/init.d/)

#service network restart

直接執行腳本或是用 service 來啟動服務,只能在本次開機階段使用,重新開機後須再重新執行一次,如果要每次開機都自動執行該服務,就要在 /etc/rc[0-6].d/ 中增加一個指向該服務腳本名稱的連結 (soft link),或是用 chkconfig 來管理。

==============================

chkconfig

chkconfig 可以管理在各執行層級的服務,包括由 xinetd 管理的服務也可以使用 chkconfig 來管理,它可以設定某個服務在那個執行層在開機是要不要啟動,例:

# chkconfig cups –list (列出 cups 服務在各執行層級的啟動設定)
cups            0:off   1:off   2:on    3:on    4:on    5:on    6:off

如果要設定該服務:
# chkconfig cups off (下次開機時 cups 不管是那個執行層級都不要啟動)
# chkconfig cups –list
cups            0:off   1:off   2:off   3:off   4:off   5:off   6:off

# chkconfig cups on (下次開機時 cups 不管是那個執行層級都啟動)
# chkconfig cups –list
cups            0:off   1:off   2:on    3:on    4:on    5:on    6:off

# chkconfig cups –level 3 off (下次開機時 cups 在執行層級 3 不要啟動)
# chkconfig cups –list
cups            0:off   1:off   2:on    3:off   4:on    5:on    6:off

chkconfig 設定只套用於下一次開機時,如果現在要啟動服務還是要直接執行該腳本或用 service 來啟動。

chkconfig 只管理 /etc/rc.d/rc[0-6].d/ 中的符號連結,不會動到腳本本身,如果要設定服務的啟動先後順序,就要到 /etc/rc.d/SERVICE_NAME 中找到 #chkconfig: 的註解行,將其後的數字作變更。

# head -5 /etc/init.d/httpd (看一下 httpd 的 chkconfig: 設定)
#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: – 85 15
(不管那個服務層級,S 設為 85,K 設為 15)

# ls -l /etc/rc.d/rc3.d/*httpd
lrwxrwxrwx 1 root root 15 Mar 28 14:45 /etc/rc.d/rc3.d/S85httpd -> ../init.d/httpd      (啟動序是 S85)

# vi /etc/init.d/httpd (將 85 改為 88)
# head -5 /etc/init.d/httpd
#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: – 88 15

# chkconfig httpd off
# chkconfig httpd on
# ls -l /etc/rc.d/rc3.d/*httpd

lrwxrwxrwx 1 root root 15 Mar 28 14:47 /etc/rc.d/rc3.d/S88httpd -> ../init.d/httpd      (已經變成 S88)