LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 912|回复: 6

[翻译]SysV和BSD啓動風格的比較

[复制链接]
发表于 2007-1-5 19:50:57 | 显示全部楼层 |阅读模式
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.  

好了,說來話長。莫謂言之不預也。

Slackware 使用BSD風格的init腳本,而很多别的發行版使用System V風格的init腳本。SysV和BSD腳本都是能讓人讀懂的,即它們都是shell腳本,而不是已編譯的程序。主要的區别在于腳本是如何設計的。

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

SysV的啓動還傾向于使用符号鏈接來組織啓動進程,例如在 /etc/rc.d/rc.4/中,可能會有指向别的目錄中的真正的腳本的各種各樣的符号鏈接。這些鏈接的命令會像是 S10network、S25xdm之類,其中的S表示啓動(start)該項服務(如果是K,則表示kill),而數字指定了腳本執行的順序。

SysV風格的啓動腳本的主要優點在于能夠設置成自動配置許多東西。例如,若你進入runlevel 6,你可以建立一個鏈接叫K75bind來終止BIND,前提是鏈接所指向的文件已經設置好來做這件事。

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

想暫時改變SysV的啓動進程也是非常痛苦的事情。假如我不想在下次啓動時運行xxx服務,最簡單的辦法是删除S10xxx這個鏈接,不算難吧?但如果我想在每個runlevel中都去掉它,我就需要從每個有關目錄中删除S10xxx這個鏈接。然後,假如我改了主意,想重新運行xxx,我需要手工重新建立所有的符号鏈接。

這樣子無疑是在已經很複雜的啓動進程上疊床架屋,而Slackware是不會這麽做的:它用BSD風格的啓動腳本。

BSD風格的腳本是直接了當的shell腳本,它們傾向于順序運行,而不需要start或stop之類參數。隻要系統進入了它們的runlevel就會執行,就這麽簡單。

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

BSD風格腳本的主要優點是它們非常容易閱讀和編輯。例如,若我想增加一個服務zzzz,我可以在 /etc/rd.d/rc.local中增加一行 /usr/local/bin/zzzz,這樣隻要是執行rc.local的runlevel,zzzz就會随之運行。假如我隻想在runlevel 4執行zzzz,我可以把它放在 /etc/rc.d/rc.4 (不是目錄,而是一個腳本)中。如果我要改變執行順序,我隻要把zzzz放在适當的服務之間,多數編輯器都支持在文件中間插入文本(就算ed都支持)。還有,你可以用注釋的方式停止一個服務,然後去掉注釋讓它重新運行。

因此,當多數發行版采用SysV風格時,Slackware采用了BSD風格。對于許多Slackware用戶,BSD風格的易用性勝過SysV風格的強大功能。當然,你可以有自己的意見。

與普遍的觀點相反,從一種風格轉到另一種并不那麽困難,隻要把inittab和rc文件從一個系統拷貝到另一個系統即可。init程序自身沒有改變,所謂“風格”多是在inittab和它所調用的腳本中設置的。

譯注:現在slackware爲了提高兼容性,在/etc/rc.d/提供了rc.sysvinit腳本以适應某些基于SysV啓動過程的商業程序的需要。另外,在許多設置服務的腳本中,也接受start、stop、restart這一類參數,例如rc.sendmail、rc.sshd等。
 楼主| 发表于 2007-1-5 19:53:13 | 显示全部楼层
似乎已经贴过了,但是找不到老的。所以重新贴下
回复 支持 反对

使用道具 举报

发表于 2007-1-5 20:07:19 | 显示全部楼层
请问可以转贴吗?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-1-5 20:11:00 | 显示全部楼层
Post by adsl
请问可以转贴吗?


of course,as your like :%
回复 支持 反对

使用道具 举报

发表于 2007-1-7 12:07:33 | 显示全部楼层
呵呵,就是我翻译的呀,不知谁给转成繁体的了。
http://www.linuxsir.cn/bbs/showthread.php?t=61525
回复 支持 反对

使用道具 举报

发表于 2007-1-7 12:24:12 | 显示全部楼层
我记得还有一篇
莫謂言之不預也

好几个人讨论这句话的翻译,呵呵
回复 支持 反对

使用道具 举报

发表于 2007-1-7 18:09:40 | 显示全部楼层
还真没看过.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表