chkconfig可以管理系統(tǒng)服務(wù)的runlevel信息,十分方便,那么對于一個自己添加的腳本,如何用chkconfig管理起來呢?
將腳本注冊為系統(tǒng)服務(wù)很簡單,只要符合script
star|stop就可以,當(dāng)然也可以添加status之類的,這樣我們就可以把腳本放在/etc/rc.d/init.d/目錄,使用service進
行start|stop等,但是這個時候還不能使用chkconfig,會提示service XXX does not support
chkconfig,那么chkconfig是如何識別系統(tǒng)服務(wù)的呢,原來很簡單:
Each service which should be manageable by
chkconfig needs two or more commented lines added to its init.d
script. The
first line tells chkconfig what runlevels the
service should be started in by default, as well as the start and stop
prior-
ity levels. If the service should not, by default,
be started in any runlevels, a - should be used in place of the run-
levels list. The second line contains a
description for the service, and may be extended across multiple lines
with back-
slash continuation.
For example, random.init has these three lines:
# chkconfig: 2345 20 80
# description: Saves and restores system entropy pool for \
# higher quality random number generation.
This says that the random script should be started
in levels 2, 3, 4, and 5, that its start priority should be 20, and that
its stop priority should be 80. You should be
able to figure out what the description says; the \ causes the line to
be
continued. The extra space in front of the line is ignored. |
也就是說,只要在腳本里面添加兩個注釋就可以了,十分簡單,大家不妨嘗試一下
Linux 的服務(wù)都是以腳本的方式運行的,這些服務(wù)的腳本都放在 /etc/rc.d/init.d
目錄下。我們可以通過修改此處的各項服務(wù)腳本控制其運行細(xì)節(jié)。
使用chkconfig 工具,可以更加簡單地控制服務(wù)的運行。
用法:
chkconfig --list [name]
chkconfig --add
chkconfig --del
chkconfig --override
chkconfig [--level ]
例如,要查看所有服務(wù)的列表:
# chkconfig --list
只查看一個服務(wù)(smb)的運行級別:
# chkconfig --list smb
smb 0:關(guān)閉 1:關(guān)閉 2:關(guān)閉 3:關(guān)閉 4:關(guān)閉 5:關(guān)閉 6:關(guān)閉
上面的這條可以清晰的顯示出在所有運行級別中,smb服務(wù)器是關(guān)閉的。我們可以用 下面的命令來打開。比如:讓smb在
3(字符)和5(圖形)模式下都開機自動運行:
# chkconfig --level 35 smb on
# chkconfig --list smb
smb 0:關(guān)閉 1:關(guān)閉 2:關(guān)閉 3:啟用 4:關(guān)閉 5:啟用 6:關(guān)閉
在配置服務(wù)訪問控制之前,需要先對 Linux 的運行級別 (runlevels)
有所了解。在不同的運行級別下,系統(tǒng)所啟用的服務(wù)也不相同,每個運行級別對應(yīng)的 /etc/rc.d/rc[x].d
目錄下包含了此運行級別下的服務(wù)。
運行級別
runlevel 0:關(guān)機,不要設(shè)置為默認(rèn)運行級
runlevel 1:單用戶模式
runlevel 2:多用戶模式,不帶NFS
runlevel 3:完全的多用戶模式,如果沒有網(wǎng)絡(luò)連接的話與2相同
runlevel 4: 未用
runlevel 5:圖形用戶模式(runlevel 3 + X 窗口系統(tǒng)“圖形用戶界面”)。
runlevel 6: 重啟,不要設(shè)成默認(rèn)運行級
如果使用命令行界面的登錄框,說明系統(tǒng)的運行級別為 3,如果使用圖形界面的登錄窗口,則說明系統(tǒng)的運行級別為 5。
通過編輯 /etc/inittab 文檔,可以更改系統(tǒng)的默認(rèn)運行級別 ,只需要將指定的運行級別替換下面這一 行中的數(shù)字 5
(運行級別 5) 即可:
id:5:initdefault:
通過編輯 /etc/inittab 完成對默認(rèn)運行級別的更改之后,新配置會在重新啟動系統(tǒng) 時后生效。
查看當(dāng)前運行級別
/sbin/runlevel
通過下面的命令來切換運行級別 (用數(shù)字如 3,5 等來替換 RUNLEVEL):
# /sbin/init RUNLEVEL