Chinaunix首页 | 论坛 | 博客
  • 博客访问: 710843
  • 博文数量: 165
  • 博客积分: 8218
  • 博客等级: 中将
  • 技术积分: 1749
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-07 19:48
文章分类

全部博文(165)

文章存档

2014年(4)

2011年(3)

2010年(6)

2009年(43)

2008年(109)

分类: LINUX

2008-09-07 20:45:42

Linux 操作系统自从开始启动至启动完毕需要经历几个不同的阶段,这几个阶段就叫做Runlevel,同样,当Linux操作系统关闭时也要经历另外几个不同的 Runlevel,下面我们就准备详细介绍一下Runlevel,并向您展示一些小技巧来让您的Linux系统避免不必要的重启动。

  Runlevel可以认为是系统状态,形象一点,您可以认为 Runlevel有点象微软的Windows操作系统中的Normal,safemode,和Command prompt only。进入每个Runlevel都需要启动或关闭相应的一系列服务(services),这些服务(services)以初始化脚本的方式放置于目录 /etc/rc.d/rc?.d/或者/etc/rc?.d下面(?代表Runlevel的对应序号)。

  在大多数的Linux发行版本中,通常有8个Runlevel

  Runlevel System State

  0 Halt the system

  1 Single user mode

  2 Basic multi user mode

  3 Multi user mode

  5 Multi user mode with GUI

  6 Reboot the system

  S, s Single user mode

  多数的桌面的Linux系统缺省的Runlevel是5,用户 登陆时是图形界面,而多数的服务器版本的Linux系统缺省的Runlevel是3,用户登陆时是字符界面,Runlevel 1和2除了调试之外很少使用,Runlevel s和S并不是直接给用户使用,而是用来为Single user mode作准备。

  Linux的运行模式比起windows的启动模式的优势在 于:你可以在系统空闲时使用.init工具切换你现在使用的Runlevel,另外,当你关闭或者启动Linux系统时你已经不知不觉中切换你的 Runlevel,系统关机进程需要调用Runlevel(0或6)来关闭所有正在运行中的进程。

  显示当前运行模式

  刚入门时,你或许对先前和目前正在使用中的运行模式很好奇,Runlevel命令可以用来显示这些信息:

  $ /sbin/Runlevel

  3 5

  Runlevel命令的输出结果表明最初的运行模式是3,而现在的运行模式是5。

  切换运行模式

  切换运行模式使用telinit命令(root用户):

  $ /sbin/telinit

  通常,切换运行模式的目的是重启某些应用进程,比如 XFree86,将运行模式从5切换到3将终止XFree86以及与之相关的所有进程,然后再把运行模式切换到5,XFree86就可以很干净的启动。在 做这些工作之前,请确保所有在XFree86下的工作都已经保存,用root用户:

  $ /sbin/telinit 3

  $ /sbin/telinit 5

  在键入命令之后,你的屏幕可能会显示一些服务启动或者停止的信息。

  在运行模式中加入启动服务

要在某个运行模式中加入一个启动服务,首先要新建该服务启动脚本, 然后把它放置于/etc/rc.d/init.d或者/etc/init.d/(根据你的Linux版本有所不同),要将该启动脚本与运行模式关联起来, 你需要这个运行模式的目录下建立一个与/etc/rc.d/init.d/下启动脚本的symbolic link,文件名的前缀通常为SXX,XX为数字,这个数字是用来控制该运行模式下服务的启动顺序。脚本的执行顺序是按照数字大小升序执行,就是数字越小 越先执行,下面就是一个在运行模式中加入启动服务具体的例子:

  $ cp myservice /etc/rc.d/init.d/

  $ ln -s /etc/rc.d/init.d/myservice /etc/rc3.d/S99myservice

  这样,下次以Runlevel 3启动时,myservice就会自动启动。

  以某个Runlevel启动

  你可以自己制定启动时所进入的Runlevel,如果你的系统使用的是lilo,在启动命令中将相应的部分为:

  LILO: Linux 5

  如果你使用的是GRUB,在启动时你可以按住e键进入设置模式,相应的位置显示为:

  kernel /vmlinuz ro root=/dev/hda1 5

ubuntu 的默认runlevel为2不是5,ubuntu也没有 /etc/inittab 文件,

在ubuntu中,inittab软件包已经被upstart软件包替换了,所有的配置信息都在/etc/event.d/目录下

这是系统中的文档

[No.524 20:56:49 sysv-rc ]$ pwd
/usr/share/doc/sysv-rc
[No.525 20:56:53 sysv-rc ]$ zcat README.runlevels.gz

Order of scripts run in /etc/rc?.d
==================================

0. Overview.

All scripts executed by the init system are located in /etc/init.d.
The directories /etc/rc?.d (? = S, 0 .. 6) contain relative links to
those scripts. These links are named S<2-digit-number>
or K<2-digit-number>.

If a scripts has the ".sh" suffix it is a bourne shell script and
MAY be handled in an optimized manner. The behaviour of executing the
script in an optimized way will not differ in any way from it being
forked and executed in the regular way.

The following runlevels are defined:

N System bootup (NONE).
S Single user mode (not to be switched to directly)
0 halt
1 single user mode
2 .. 5 multi user mode
6 reboot

1. Boot.

When the systems boots, the /etc/init.d/rcS script is executed. It
in turn executes all the S* scripts in /etc/rcS.d in alphabetical
(and thus numerical) order. The first argument passed to the
executed scripts is "start". The runlevel at this point is "N" (none).

Only things that need to be run once to get the system in a consistent
state are to be run. The rcS.d directory is NOT meant to replace rc.local.
One should not start daemons in this runlevel unless absolutely
necessary. Eg, NFS might need the portmapper, so it is OK to start it
early in the bootprocess. But this is not the time to start the
squid proxy server.

2. Going multiuser.

After the rcS.d scripts have been executed, init switches to the
default runlevel as specified in /etc/inittab, usually "2".

Init then executes the /etc/init.d/rc script which takes care of
starting the services in /etc/rc2.d.

Because the previous runlevel is "N" (none) the /etc/rc2.d/KXXxxxx
scripts will NOT be executed - there is nothing to stop yet,
the system is busy coming up.

If for example there is a service that wants to run in runlevel 4
and ONLY in that level, it will place a KXXxxxx script in
/etc/rc{2,3,5}.d to stop the service when switching out of runlevel 4.
We do not need to run that script at this point.

The /etc.rc2.d/SXXxxxx scripts will be executed in alphabetical
order, with the first argument set to "start".

3. Switching runlevels.

When one switches from (for example) runlevel 2 to runlevel 3,
/etc/init.d/rc will first execute in alphabetical order all K
scripts for runlevel 3 (/etc/rc3.d/KXXxxxx) with as first argument
"stop" and then all S scripts for runlevel 3 (/etc/rc3.d/SXXxxxx)
with as first argument "start".

As an optimization, a check is made for each "service" to see if
it was already running in the previous runlevel. If it was, and there
is no K (stop) script present for it in the new runlevel, there is
no need to start it a second time so that will not be done.

On the other hand, if there was a K script present, it is assumed the
service was stopped on purpose first and so needs to be restarted.

We MIGHT make the same optimization for stop scripts as well-
if no S script was present in the previous runlevel, we can assume
that service was not running and we don't need to stop it either.
In that case we can remove the "coming from level N" special case
mentioned above in 2). But right now that has not been implemented.

4. Single user mode.

Switching to single user mode is done by switching to runlevel 1.
That will cause all services to be stopped (assuming they all have
a K script in /etc/rc1.d). The runlevel 1 scripts will then switch
to runlevel "S" which has no scripts - all it does is spawn
a shell directly on /dev/console for maintenance.

5. Halt/reboot

Going to runlevel 0 or 6 will cause the system to be halted or rebooted,
respectively. For example, if we go to runlevel 6 (reboot) first
all /etc/rc6.d/KXXxxxx scripts will be executed alphabetically with
"stop" as the first argument.

Then the /etc/rc6.d/SXXxxxx scripts will be executed alphabetically
with "stop" as the first argument as well. The reason is that there
is nothing to start anymore at this point - all scripts that are
run are meant to bring the system down.

In the future, the /etc/rc6.d/SXXxxxx scripts MIGHT be moved to
/etc/rc6.d/K1XXxxxx for clarity.


[No.527 21:00:23 sysv-rc ]$ apt-file search README.runlevels.gz
sysv-rc: usr/share/doc/sysv-rc/README.runlevels.gz
sysvinit: usr/share/doc/sysvinit/README.runlevels.gz
阅读(2897) | 评论(0) | 转发(0) |
0

上一篇:一个超级 vimrc

下一篇:vim user manual

给主人留下些什么吧!~~