CentOS LInux啟動關閉和服務管理(zt)

來源:互聯網
上載者:User

本文轉自

(超詳細)http://bbs.ywlm.net/thread-615-1-1.html

http://hi.baidu.com/edeed/blog/item/76b81e17dd9545054a90a705.html

http://jiajun.iteye.com/blog/387265

CentOS啟動順序:

1.當我們按下電源開關,把電源投入到機器中後,首先被啟動執行的就是這個BIOS(BasicInput/Output System)程式。BIOS的功能是......並且訪問硬碟先頭512Bit的MBR(Master Boot Record)。 2.經BOIS的控制,將MBR中的Boot Record調入記憶體中。這裡就要說說Linux的Boot程式了,Linux的Boot程式有GRUB和LILO,而CentOS預設的Boot程式是GRUB。 3.通過Boot程式,訪問硬碟中的Linux核心程式。 4.將核心程式調入記憶體中。 5.核心程式調用完成後,通過核心訪問硬碟中將要用到的其他檔案。 核心第一個執行的檔案是/sbin/init,而這個檔案的設定和定義檔案是/etc/inittab這個檔案,也就是說/sbin/init按照/etc/inittab的定義來執行下一步的啟動,那我們就要看看/etc/inittab檔案是怎麼回事兒了。

[root@linux ~]# cat -n /etc/inittab     1   #     2   # inittab    This file describes how the INIT process should set up     3   #             the system in a certain run-level.     4   # 5 # Author: Miquel van Smoorenburg,
<miquels@drinkel.nl.mugnet.org>     6   #             Modified for RHS Linux by Marc Ewing and Donnie Barnes     7   #     8      9   # Default runlevel. The runlevels used by RHS are: 10   # 0 - halt (Do NOT set initdefault to this) 11   # 1 - Single user mode 12   # 2 - Multiuser, without NFS (The same as 3, if you do not have networking) 13   # 3 - Full multiuser mode 14   # 4 - unused 15   # 5 - X11 16   # 6 - reboot (Do NOT set initdefault to this) 17   # 18   id:3:initdefault: ← 預設的啟動模式 19  20   # System initialization. 21   si::sysinit:/etc/rc.d/rc.sysinit ← boot時的處理 22  23   l0:0:wait:/etc/rc.d/rc 0     ←   23-29行,各啟動模式的處理,分別啟動/etc/rc.d/rcX.d指令碼 24   l1:1:wait:/etc/rc.d/rc 1 25   l2:2:wait:/etc/rc.d/rc 2 26   l3:3:wait:/etc/rc.d/rc 3 27   l4:4:wait:/etc/rc.d/rc 4 28   l5:5:wait:/etc/rc.d/rc 5 29   l6:6:wait:/etc/rc.d/rc 6 30  31   # Trap CTRL-ALT-DELETE 32   ca::ctrlaltdel:/sbin/shutdown -t3 -r now ← Ctrl+Alt+Del被啟用時的處理 33  34   # When our UPS tells us power has failed, assume we have a few minutes 35   # of power left.   Schedule a shutdown for 2 minutes from now. 36   # This does, of course, assume you have powerd installed and your 37   # UPS connected and working correctly.  38   pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down" ← 電源off時的處理 39  40   # If power was restored before the shutdown kicked in, cancel it. 41   pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled" ← 電源on時的處理 42  43  44   # Run gettys in standard runlevels ←   45-50行是6個虛擬終端 45   1:2345:respawn:/sbin/mingetty tty1 46   2:2345:respawn:/sbin/mingetty tty2 47   3:2345:respawn:/sbin/mingetty tty3 48   4:2345:respawn:/sbin/mingetty tty4 49   5:2345:respawn:/sbin/mingetty tty5 50   6:2345:respawn:/sbin/mingetty tty6 51  52   # Run xdm in runlevel 5 53   x:5:respawn:/etc/X11/prefdm -nodaemon ← 啟動模式5的時候,啟動/etc/X11視窗系統 [root@linux ~]#

在以上的這個檔案中,#開頭的檔案是注釋檔案,可以忽略不看,但是能夠很好的幫我們瞭解檔案。 這個檔案中的定義形勢如下: 通過下表的說明去看這個檔案,就清晰明了的多了! <id>:<runlevel>:<action>:<process>

   id 不重複的任一數字                                                                  
runlevel 0~6的啟動層級(模式)
action
initdefault 定義預設時的啟東層級(模式)
sysinit 引導(boot)中執行
wait 引導中執行一次,直到init進程為止
respawn 進程停止的情況下,再啟動
powerfall 當接收到電源異常訊號時,執行
ctrlaltdel 當接收到Ctrl+Alt+Del指令時,執行
process

定義被執行的命令

 

通過上面的說明,我想你應該可以明白/etc/inittab中的定義是怎麼一回事兒了。下面我們重點看18,21,26行(其他的自己研究)。通過上表我們可以知道:

 

 

  • 18行的定義是預設的啟動層級是“3“文字模式。
  • 21行的定義是啟動的過程中執行/etc/rc.d/rc.sysinit檔案(啟動層級是空,也就是說無論什麼層級都執行這個檔案),這個檔案很長有差不多1000行的內容,主要功能是啟動網路,讀取檔案系統,使swap有效化和調入模組等動作。
  • 26行的定義和上面的18行的定義是對應的,也就是說18行定義的啟動層級是"X",那麼就執行檔案23到29行定義中的"X"。

   這裡18行定義的是啟動層級3,就執行26行中啟動層級3的定義,執行/etc/rc.d/rc指令檔(控制檔案),並將18行定義的啟動層級以參數的形勢交給/etc/rc.d/rc檔案進行處理。這裡我們看到“l3:3:wait:/etc/rc.d/rc 3”是把啟動層級3交給檔案rc處理,那我們有必要看看rc檔案都定義了什麼。 /etc/rc.d/rc檔案內容如下:

[root@linux ~]# cat -n /etc/rc.d/rc     1   #! /bin/bash     2   #     3   # rc          This file is responsible for starting/stopping     4   #             services when the runlevel changes.              5   #     6   # Original Author:   
    7   #             Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>     8   #     9  10   # check a file to be a correct runlevel script 11   check_runlevel () 12   { 13       # Check if the file exists at all. 14       [ -x "$1" ] || return 1 15  16       # Reject backup files and files generated by rpm. 17       case "$1" in 18                *.rpmsave|*.rpmorig|*.rpmnew|*~|*.orig) 19                          return 1 20                          ;; 21       esac 22       return 0 23   } 24  25   # Now find out what the current and what the previous runlevel are. 26   argv1="$1" 27   set `/sbin/runlevel` 28   runlevel=$2 29   previous=$1 30   export runlevel previous 31  32   . /etc/init.d/functions 33  34   # See if we want to be in user confirmation mode 35   if [ "$previous" = "N" ]; then 36       if [ -f /var/run/confirm ]; then 37                echo $"Entering interactive startup" 38       else 39                echo $"Entering non-interactive startup" 40       fi 41   fi 42  43   # Get first argument. Set new runlevel to this argument. 44   [ -n "$argv1" ] && runlevel="$argv1" 45  46   # Is there an rc directory for this new runlevel? 47   [ -d /etc/rc$runlevel.d ] || exit 0 48  49   # First, run the KILL scripts. 50   for i in /etc/rc$runlevel.d/K* ; do 51       check_runlevel "$i" || continue 52  53       # Check if the subsystem is already up. 54       subsys=${i#/etc/rc$runlevel.d/K??} 55       [ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] \ 56                || continue 57  58       # Bring the subsystem down. 59       if egrep -q "(killproc |action )" $i ; then 60                $i stop 61       else 62                action $"Stopping $subsys: " $i stop 63       fi 64   done 65  66   # Now run the START scripts. 67   for i in /etc/rc$runlevel.d/S* ; do 68       check_runlevel "$i" || continue 69  70       # Check if the subsystem is already up. 71       subsys=${i#/etc/rc$runlevel.d/S??} 72       [ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] \ 73                && continue 74                   75       # If we're in confirmation mode, get user confirmation 76       if [ -f /var/run/confirm ]; then 77                confirm $subsys 78                test $? = 1 && continue 79       fi 80  81       update_boot_stage "$subsys" 82       # Bring the subsystem up. 83       if [ "$subsys" = "halt" -o "$subsys" = "reboot" ]; then 84                export LC_ALL=C 85                exec $i start 86       fi 87       if egrep -q "(daemon |action |success |failure )" $i 2>/dev/null \ 88                          || [ "$subsys" = "single" -o "$subsys" = "local" ]; then 89                $i start 90       else 91                action $"Starting $subsys: " $i start 92       fi 93   done 94   rm -f /var/run/confirm 95   if [ -x /usr/bin/rhgb-client ] && /usr/bin/rhgb-client --ping ; then 96 /usr/bin/rhgb-client --quit 97   fi [root@linux ~]#

在這個檔案中我們看看如下的幾行:

  • 50行以K開頭的指令檔
  • 60行執行stop
  • 67行以S開頭的指令檔
  • 91行執行start

在這個檔案中,每個程式塊的部分都有注釋(#開頭的行),如果有興趣的話可以自行研究一下。似乎不是那麼太難,就是check→do,確認後執行,判斷後執行的樣子。
rc.d目錄下面的檔案:

[root@linux ~]# ls -l /etc/rc.d 總用量 112 drwxr-xr-x   2 root root   4096 12月 28 12:45 init.d -rwxr-xr-x   1 root root   2352 2004-03-17   rc drwxr-xr-x   2 root root   4096 12月 28 12:45 rc0.d drwxr-xr-x   2 root root   4096 12月 28 12:45 rc1.d drwxr-xr-x   2 root root   4096 12月 28 12:45 rc2.d drwxr-xr-x   2 root root   4096 12月 28 12:45 rc3.d drwxr-xr-x   2 root root   4096 12月 28 12:45 rc4.d drwxr-xr-x   2 root root   4096 12月 28 12:45 rc5.d drwxr-xr-x   2 root root   4096 12月 28 12:45 rc6.d -rwxr-xr-x   1 root root 220 2003-06-24   rc.local -rwxr-xr-x   1 root root 27584   8月 13 17:10 rc.sysinit [root@linux ~]#

我們看到了,在這個目錄下面,有剛才提到的rc檔案,還有另外的一些檔案,我們來簡單說明一下。
1. init.d    這個不是檔案,是一個目錄,這個目錄下面存放著各各服務的控制指令碼,這下面的檔案和你安裝了些什麼軟體包有關係。如果你有興趣你可以察看他們的指令檔,接下來的說明中我們還會說到他,因為/etc/rc.d/rcX.d下的檔案和這個init.d下面的檔案是通過軟串連相連的。
2. rc    這個檔案我們上面提到過,跳過了。
3. rc.loca    這個檔案也許會用到,如果你安裝了一些軟體或服務,並非系統標準的服務,比如手動的安裝了Httpd,這樣你用chkconfig命令是無法操作httpd的,開啟伺服器的時候也並不預設的啟動這個httpd服務,這個時候你就可以將啟動命令寫到這個檔案中,讓開機啟動服務完畢之後,最後啟動這個httpd服務,注意是最後。這裡說的httpd只是一個例子,將來也許會有很多的自己安裝的服務會用到這個檔案,知道一下,要用到的時候自然就明白。當然還有另外一種方法,就是自己寫一個啟動指令檔,讓系統開機時讀取指令檔來對httpd進行啟動操作
4. rc.sysini    我們上面提到過這個檔案,這個檔案是在boot的時候就被執行的指令碼,它的任務是初始化系統的網路,設定hostname,歡迎資訊表示,時鐘設定,掛載檔案系統等。有興趣可以讀它的指令檔。
5. rcX.d    這個rcX.d裡面的X代表了0~6的數字(6種啟動模式)。我們看到了它們都是目錄,下面放著的都是我們上面說的/etc/rc.d/init.d下面檔案的link。這裡我們已rc3.d為例子,簡單說明一下。    在察看這個檔案的時候注意2點: ##第一:ls -l 察看它們的詳細資料,看看他們的link指向 ##第二:ls -l 察看它們的檔案名稱的頭字母,形式應該是這樣的[S或K <數字> <名稱>]的形勢。S代表啟動,K代錶停止。    不要迷糊,/etc/rc.d/rc就定義了這個檔案名稱頭文字的“S”和“K”。也就是開機的時候,已S開頭的指令檔別執行,這個服務就被開機運行;那麼已K開頭的檔案不被執行,這個檔案所控制的服務也不被執行,這個檔案控制的服務也不被開機運行。    這樣看來,我們在給伺服器左右化的時候,就可以直接的將相應的啟動模式(rc0.d~rc6.d)下的相應服務控制指令碼的檔案名稱改掉(S改成K,K改成S),就能夠簡單的最佳化開機時系統將運行哪兒些個服務。    如果通過命令來控制的話,那命令是chkconfig命令.......編輯中......
   如果你察看過rc0.d~rc6.d的目錄下的檔案名稱,你會發現,他們以S開都得檔案名稱數量是不同的。   /etc/rc.d/rc3.d下都有什麼(一部分而已,你可以在自己的系統下面察看全部)

[root@linux ~]# ls -l /etc/rc.d/rc3.d 合計 224 lrwxrwxrwx   1 root root 21   1月   5 05:24 K01tog-pegasus -> ../init.d/tog-pegasus lrwxrwxrwx   1 root root 13   1月   5 05:12 K01yum -> ../init.d/yum lrwxrwxrwx   1 root root 24   1月   5 05:12 K02NetworkManager -> ../init.d/NetworkManager lrwxrwxrwx   1 root root 15   1月   5 05:12 K03rhnsd -> ../init.d/rhnsd lrwxrwxrwx   1 root root 19   1月   5 05:08 K05saslauthd -> ../init.d/saslauthd lrwxrwxrwx   1 root root 16   1月   5 05:11 K10psacct -> ../init.d/psacct lrwxrwxrwx   1 root root 17   1月   5 05:25 K12FreeWnn -> ../init.d/FreeWnn lrwxrwxrwx   1 root root 13   1月   5 05:12 K20nfs -> ../init.d/nfs lrwxrwxrwx   1 root root 14   1月   5 05:11 K24irda -> ../init.d/irda lrwxrwxrwx   1 root root 16   1月   5 05:10 K50ibmasm -> ../init.d/ibmasm lrwxrwxrwx   1 root root 17   1月   5 05:12 K50netdump -> ../init.d/netdump lrwxrwxrwx   1 root root 16   1月   5 05:26 K73ypbind -> ../init.d/ypbind [root@linux ~]#

接下來我們看看控制服務的一些工具 chkconfig setup system-config-services 我們分別來看看這3個工具(如果還有別的,告訴我)
chkconfig 這個工具我們很熟悉,並且在我們的網站配置祥解中也提到過他的使用。 執行許可權:root 命令路徑:/sbin/chkconfig 使用法:        chkconfig <服務名> on          #這個和接下來的off這個其實就是把以K開頭的指令檔從命名       chkconfig <服務名> off          #如果你有興趣自己實驗一下就知道了       chkconfig --list [服務名]       chkconfig --add <服務名>       chkconfig --del <服務名>       chkconfig [--level <啟動層級>] <服務名> <on|off|reset|resetpriorities>
setup 是系統綜合的組態工具,命令列下也可以用。這個不多說,操作一下就知道怎麼回事了。
system-config-services 如果你安裝了gnome這樣的視窗案頭系統,你也可以使用這個工具來體驗下滑鼠點擊帶來的方便。(如果你安裝了案頭系統,可以# system-config加Tab鍵,會有很多工具的執行命令,體會下。)
主要服務參照: http://www.centospub.com/bbs/viewthread.php?tid=1851&extra=page%3D1
以上是Linux系統(CentOS)的啟動流程和服務管理相關的一點兒知識,如果有不足大家補充。非常感謝錯誤修正的朋友們,感謝你們的支援。

from:http://www.centospub.com/bbs/viewthread.php?tid=1295&highlight=

--End--

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.