機(jī)電之家資源網(wǎng)
單片機(jī)首頁|單片機(jī)基礎(chǔ)|單片機(jī)應(yīng)用|單片機(jī)開發(fā)|單片機(jī)文案|軟件資料下載|音響制作|電路圖下載 |嵌入式開發(fā)
培訓(xùn)信息
贊助商
SysV和BSD啟動(dòng)風(fēng)格的比較(中英文對照)
SysV和BSD啟動(dòng)風(fēng)格的比較(中英文對照)
 更新時(shí)間:2009-8-12 16:52:56  點(diǎn)擊數(shù):0
【字體: 字體顏色

 What's the difference between Slackware startup scripts and System V startup scripts?

Okay, this answer is very long. Just a warning.

Slackware uses BSD-style init scripts; many other distros use System V-style init scripts. Both SysV scripts and BSD scripts are human-readable, in that they are shell scripts, not compiled programs. The main difference is in how the scripts are designed.

SysV scripts tend to take arguments like start, stop, restart, and others, depending on the program it's starting. So you could say something like /etc/ init.d/bind start to start BIND, and /etc/init.d/bind stop to stop BIND.

SysV-style init also tends to use symlinks to organize the boot process: in /etc/rc.d/rc.4/, there might be various symlinks to actual scripts in another directory. The symlinks are named like S10network, S25xdm, and so on, where the S means to start the service (K means kill it), and the numbers designate the order in which scripts should run.

The main advantage of SysV style init scripts is that they can be set up to configure a lot of stuff automagically. If, for example, you go into runlevel 6, you can have a symlink in /etc/rc.d/rc.6/ called K75bind, which will kill off BIND if the file to which its linked is set up to do that.

The main disadvantage of SysV style is that it's terribly convoluted. If I want to add a service, for example, I need to write a SysV-style script (which is certainly nontrivial) to at least handle "start" (and possibly "stop"). Then I need to make sure I've got the symlink set up correctly in each runlevel where I want it to run, and if I happen to need it to execute between two scripts that are consecutively numbered, I need to do some symlink renumbering (e.g., if S10xxx and S11yyy exist, and I want zzzz to run between, I need to resymlink one of those files to squeeze zzzz between them).

It's also a huge pain to alter the SysV boot process temporarily--if I want service xxx to not run on next boot, the easiest way is to remove the S10xxx symlink. Not too hard, but if I want to remove it from every runlevel, I need to remove the S10xxx symlink from every directory. Then if I change my mind and want xxx to run again, I need to recreate all of the appropriate symlinks by hand.

It's one extra level of complexity to the already-complicated boot process, and one which Slackware doesn't use: it uses BSD-style startup scripts instead.

BSD-style scripts are straight-ahead shell scripts that tend to run sequentially and don't take arguments like start or stop. They run when the system enters their runlevel, and that's it.

The main disadvantage of BSD-style is that you have to use some other method of controlling daemons. For example, if I want to stop BIND, I need to ps ax|grep named, find named's PID, and kill the pid. (Or I can find the pid file.) But I can't say /etc/init.d/bind stop (unless I write a SysV-style script for that).

The main advantage of BSD-style scripts is that they're terribly easy to read and edit. For example, if I add a new service zzzz, I can add the line /usr/ local/bin/zzzz to /etc/rc.d/rc.local, and zzzz will run in the runlevels where rc.local executes. If I know I want zzzz only in runlevel 4, I can put it in /etc/ rc.d/rc.4 (no longer a directory, but a shell script). If I need to change the order, I can just put the call to zzzz between the services where it should run; most editors can handle inserting text in the middle of a file (even ed!). Also, you can easily comment out a service to stop it from running, and uncomment it later.

So, while most distributions use SysV style, Slackware uses BSD-style. For many Slackware users, the ease-of-use of the BSD-style greatly outweighs the power of SysV-style. You can certainly form your own opinion.

Contrary to popular belief, it's not that difficult to switch from one style to the other--just grab the inittab and rc files from one system and copy them to another. The init binary itself is not changed--most of the ''style'' is set in inittab and the scripts it calls.  

好了,說來話長。莫謂言之不預(yù)也。

Slackware 使用BSD風(fēng)格的init腳本,而很多別的發(fā)行版使用System V風(fēng)格的init腳本。SysV和BSD腳本都是能讓人讀懂的,即它們都是shell腳本,而不是已編譯的程序。主要的區(qū)別在于腳本是如何設(shè)計(jì)的。

SysV腳本傾向于接受諸如start、stop、restart之類的參數(shù),依它所啟動(dòng)的程序而定。所以你可以用 /etc/init.d/bind start 這樣的命令來啟動(dòng)BIND,并用 /etc/init.d/bind stop 來停止BIND。

SysV的啟動(dòng)還傾向于使用符號(hào)鏈接來組織啟動(dòng)進(jìn)程,例如在 /etc/rc.d/rc.4/中,可能會(huì)有指向別的目錄中的真正的腳本的各種各樣的符號(hào)鏈接。這些鏈接的命令會(huì)像是 S10network、S25xdm之類,其中的S表示啟動(dòng)(start)該項(xiàng)服務(wù)(如果是K,則表示kill),而數(shù)字指定了腳本執(zhí)行的順序。

SysV風(fēng)格的啟動(dòng)腳本的主要優(yōu)點(diǎn)在于能夠設(shè)置成自動(dòng)配置許多東西。例如,若你進(jìn)入runlevel 6,你可以建立一個(gè)鏈接叫K75bind來終止BIND,前提是鏈接所指向的文件已經(jīng)設(shè)置好來做這件事。

SysV風(fēng)格腳本的主要缺點(diǎn)是太過彎彎繞。假如我想增加一個(gè)服務(wù),我要先寫一個(gè)SysV風(fēng)格的腳本(不是容易的事),它至少要處理“start”(還可能有“stop”)。然后,我必須確保在每個(gè)要運(yùn)行這個(gè)服務(wù)的runlevel中正確地設(shè)置好符號(hào)鏈接。如果恰好這個(gè)服務(wù)需要在已經(jīng)連續(xù)編號(hào)的兩個(gè)腳本之間運(yùn)行,我就需要做一些對符號(hào)鏈接重新編號(hào)的工作(例如,S10xxx和S11yyy已經(jīng)存在,而我想讓zzzz在它們之間運(yùn)行,我就需要對前兩者之一重新建立符號(hào)鏈接來把zzzz擠進(jìn)去)。

想暫時(shí)改變SysV的啟動(dòng)進(jìn)程也是非常痛苦的事情。假如我不想在下次啟動(dòng)時(shí)運(yùn)行xxx服務(wù),最簡單的辦法是刪除S10xxx這個(gè)鏈接,不算難吧?但如果我想在每個(gè)runlevel中都去掉它,我就需要從每個(gè)有關(guān)目錄中刪除S10xxx這個(gè)鏈接。然后,假如我改了主意,想重新運(yùn)行xxx,我需要手工重新建立所有的符號(hào)鏈接。

這樣子無疑是在已經(jīng)很復(fù)雜的啟動(dòng)進(jìn)程上疊床架屋,而Slackware是不會(huì)這么做的:它用BSD風(fēng)格的啟動(dòng)腳本。

BSD風(fēng)格的腳本是直接了當(dāng)?shù)膕hell腳本,它們傾向于順序運(yùn)行,而不需要start或stop之類參數(shù)。只要系統(tǒng)進(jìn)入了它們的runlevel就會(huì)執(zhí)行,就這么簡單。

BSD風(fēng)格的主要缺點(diǎn)是你需要一些其他方法來控制后臺(tái)服務(wù)。例如,若我要停止BIND,我要先用命令 ps ax|grep named 找出 named的PID,然后kill這個(gè)PID(或者用這個(gè)pid的文件名)。但是我不能簡單地下個(gè)命令 /etc/init.d/bind stop (除非我已經(jīng)寫了個(gè)這樣的SysV腳本)。

BSD風(fēng)格腳本的主要優(yōu)點(diǎn)是它們非常容易閱讀和編輯。例如,若我想增加一個(gè)服務(wù)zzzz,我可以在 /etc/rd.d/rc.local中增加一行 /usr/local/bin/zzzz,這樣只要是執(zhí)行rc.local的runlevel,zzzz就會(huì)隨之運(yùn)行。假如我只想在runlevel 4執(zhí)行zzzz,我可以把它放在 /etc/rc.d/rc.4 (不是目錄,而是一個(gè)腳本)中。如果我要改變執(zhí)行順序,我只要把zzzz放在適當(dāng)?shù)姆⻊?wù)之間,多數(shù)編輯器都支持在文件中間插入文本(就算ed都支持)。還有,你可以用注釋的方式停止一個(gè)服務(wù),然后去掉注釋讓它重新運(yùn)行。

因此,當(dāng)多數(shù)發(fā)行版采用SysV風(fēng)格時(shí),Slackware采用了BSD風(fēng)格。對于許多Slackware用戶,BSD風(fēng)格的易用性勝過SysV風(fēng)格的強(qiáng)大功能。當(dāng)然,你可以有自己的意見。

與普遍的觀點(diǎn)相反,從一種風(fēng)格轉(zhuǎn)到另一種并不那么困難,只要把inittab和rc文件從一個(gè)系統(tǒng)拷貝到另一個(gè)系統(tǒng)即可。init程序自身沒有改變,所謂“風(fēng)格”多是在inittab和它所調(diào)用的腳本中設(shè)置的。

譯注:現(xiàn)在slackware為了提高兼容性,在/etc/rc.d/提供了rc.sysvinit腳本以適應(yīng)某些基于SysV啟動(dòng)過程的商業(yè)程序的需要。另外,在許多設(shè)置服務(wù)的腳本中,也接受start、stop、restart這一類參數(shù),例如rc.sendmail、rc.sshd等。

  • 上一篇: Linux下添加硬盤,分區(qū),格式化詳解
  • 下一篇: Linux解釋器原理
  • 發(fā)表評(píng)論   告訴好友   打印此文  收藏此頁  關(guān)閉窗口  返回頂部
    熱點(diǎn)文章
     
    推薦文章
     
    相關(guān)文章
    網(wǎng)友評(píng)論:(只顯示最新5條。)
    關(guān)于我們 | 聯(lián)系我們 | 廣告合作 | 付款方式 | 使用幫助 | 機(jī)電之家 | 會(huì)員助手 | 免費(fèi)鏈接

    點(diǎn)擊這里給我發(fā)消息66821730(技術(shù)支持)點(diǎn)擊這里給我發(fā)消息66821730(廣告投放) 點(diǎn)擊這里給我發(fā)消息41031197(編輯) 點(diǎn)擊這里給我發(fā)消息58733127(審核)
    本站提供的機(jī)電設(shè)備,機(jī)電供求等信息由機(jī)電企業(yè)自行提供,該企業(yè)負(fù)責(zé)信息內(nèi)容的真實(shí)性、準(zhǔn)確性和合法性。
    機(jī)電之家對此不承擔(dān)任何保證責(zé)任,有侵犯您利益的地方請聯(lián)系機(jī)電之家,機(jī)電之家將及時(shí)作出處理。
    Copyright 2007 機(jī)電之家 Inc All Rights Reserved.機(jī)電之家-由機(jī)電一體化網(wǎng)更名-聲明
    電話:0571-87774297 傳真:0571-87774298
    杭州濱興科技有限公司提供技術(shù)支持

    主辦:杭州市高新區(qū)(濱江)機(jī)電一體化學(xué)會(huì)
    中國行業(yè)電子商務(wù)100強(qiáng)網(wǎng)站

    網(wǎng)站經(jīng)營許可證:浙B2-20080178-1