busybox下inittab中runlevel解析
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><original-name>
or K<2-digit-number><original-name>.
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.
/etc/rc?.d目录下脚本运行顺序
===========================
0.概要
所有init system机制要执行的脚本都存放在/etc/init.d目录下。/etc/rc?.d(?=S,0..6)目录包含符号链接到/etc/init.d目录下的脚本。这些符号链接以S<2-digit-number><original-name>或者K<2-digit-number><original-name>命名。
假如一个脚本以.sh后缀结尾,那么它是bourne shell并且会以最佳的方式处理。
运行模式定义如下:
N System bootup (NONE)
S Single user mode
0 halt
1 single user mode
2..5 multi user mode
6 reboot
1.boot
当系统引导时,/etc/init.d/rcS脚本会被执行,然后按顺序执行/etc/rcS.d目录下的脚本(按数字大小顺序,从小到大)。传给执行脚本的第一个参数时"start"。此时的运行等级时N。
注意:只有那些被系统执行一次的脚本才放在这个阶段。除非特殊需要,否则守护进程不要放在这个运行等级(N)执行。比如,NFS需要端口映射,所以在boot阶段开启它时OK的。但是开启squid代理服务器就不行。
2.进入multiuser(多用户模式)
当执行完rcS.d脚本,再回到inittab继续解析,一般init切换到默认的运行等级,Linux发行版的运行等级默认通常是2.
init然后执行/etc/init.d/rc脚本,这个脚本中会解析到开始运行/etc/rc2.d目录下的服务脚本。
由于之前运行等级时N,所以/etc/rc2.d/KXXxxxx脚本不会被执行(因为系统刚刚启动,没有什么服务可停止的)。
假如比方说有一个服务想运行且只运行在运行等级4,那么它会放一个KXXxxxx脚本在/etc/rc{2,3,5}.d目录下用于从运行等级4切换到运行等级2或3或5时停止这个服务。因为在运行等级2,3,5不需要这个服务脚本。
当第一个参数设置为"start",/etc.rc2.d/SXXxxxx脚本将会按顺序执行
3.切换运行等级
当从一个运行等级比如2切换到3时,/etc/init.d/rc脚本首先按顺序执行所有在运行等级3的K脚本(即/etc/rc3.d/KXXxxxx),然后执行运行等级3的S脚本(即/etc/rc3.d/SXXxxxx)
作为优化,将会对每一个服务做检查看在之前的运行等级是否已经在运行该服务。如果是在运行并且在切换后的运行等级中没有K(stop)脚本,那就没必要重新开启那个服务。
相反,如果在切换后的运行等级中有K脚本,那么它将先停止这个服务。
一样的我们可以这样优化停止脚本,假如在之前的运行等级中没有S脚本,那么我们可以认为该服务没有运行我们没必要停止该服务。
4.单用户模式
切换到单用户模式是通过切换到运行等级1.这将导致所有的服务被停止(即在/etc/rc1.d目录下有所有的K脚本)。然后运行等级1的脚本将会被切换到运行等级S,运行等级S中没有任何脚本,它所做的就是直接在/dev/console控制台上产生一个shell用于维护
5.Halt/reboot 停机/重启
进入运行等级0或6将会导致系统进入停机或重启。比如,如果我们进入到运行等级6,首先所有的/etc/rc6.d/KXXxxxx脚本会按顺序执行。
然后/etc/rc6.d/SXXxxxx脚本将按顺序执行。
busybox下inittab中runlevel解析的更多相关文章
- Linux 下shell中exec解析
exec和source都属于bash内部命令(builtins commands),在bash下输入man exec或man source可以查看所有的内部命令信息. bash shell ...
- 在用busybox制作系统过程中遇到的问题
遇到的问题: 1.开机报错: 在做完整个系统之后重启出现了这个报错 VFS: Cannot open root device "sda2" or unknown-block(0,0 ...
- linux系统初始化——busybox的inittab文件格式说明
busybox的inittab文件格式说明 要写自己的inittab,需要理解busybox的inittab文件格式. busybox的inittab文件与通常的inittab不同,它没有runlev ...
- 简单理解Busybox下halt/poweroff/reboot实现及区别
关键词:halt/poweroff/reboot.reboot().SIGUSR1/SIGTERM/SIGUSR2等. 1. busybox下的halt/poweroff/reboot实现 通过app ...
- Busybox下tftp命令使用详解
http://blog.chinaunix.net/uid-375398-id-1991686.html Busybox下的tftp命令可以用来进行单文件传输.使用的时候,是把电脑作为服务器Serve ...
- WCF中配置文件解析
WCF中配置文件解析[1] 2014-06-14 WCF中配置文件解析 参考 WCF中配置文件解析 返回 在WCF Service Configuration Editor的使用中,我们通过配置工具自 ...
- Hadoop 中疑问解析
Hadoop 中疑问解析 FAQ问题剖析 一.HDFS 文件备份与数据安全性分析1 HDFS 原理分析1.1 Hdfs master/slave模型 hdfs采用的是master/slave模型,一个 ...
- (转)springMVC框架下JQuery传递并解析Json数据
springMVC框架下JQuery传递并解析Json数据 json作为一种轻量级的数据交换格式,在前后台数据交换中占据着非常重要的地位.Json的语法非常简单,采用的是键值对表示形式.JSON 可以 ...
- JAVA方法调用中的解析与分派
JAVA方法调用中的解析与分派 本文算是<深入理解JVM>的读书笔记,参考书中的相关代码示例,从字节码指令角度看看解析与分派的区别. 方法调用,其实就是要回答一个问题:JVM在执行一个方法 ...
随机推荐
- [POJ 2329] Nearest number-2
Link: POJ 2329 传送门 Solution: 比较明显的$dp$,但爆搜好像也能过 用多个方向$dp$来解决此题,最后汇总答案即可 一开始我写了4个,但后来发现只要相反的2个方向即可,同时 ...
- CentOS 6.9设置IP、网关、DNS
在/etc/sysconfig/network-scripts目录下,用vi命令编辑eth0: DEVICE=eth0 #设备名称 HWADDR=C8:1F:66:B8:EB:9F #物理地址 ONB ...
- mysql-essential-5.1.55-win32 安装
1.选择无事物安装 2.my.cnf [mysqld] default-storage-engine=INNODB innodb=on 3.设置数据目录 手动创建目录 D:\data [mysqld] ...
- OVF? OVA? VMDK? – File Formats and Tools for Virtualization
I recently worked on a project to create a “virtual appliance” for one of our customers. They have a ...
- iOS教程:如何使用Core Data – 预加载和引入数据
这是接着上一次<iOS教程:Core Data数据持久性存储基础教程>的后续教程,程序也会使用上一次制作完成的. 再上一个教程中,我们只做了一个数据模型,之后我们使用这个数据模型中的数据创 ...
- 重要的ui组件——Behavior
v7包下的组件类似CoordinatorLayout推出也有一段时间了,大家使用的时候应该会体会到其中很多的便利,今天这篇文章带大家来了解一个比较重要的ui组件——Behavior.从字面意思上就可以 ...
- 联想台式机启天m4350 启用intel vt-x
在vmware workstations10 64位上安装windows server 2012操作系统时,出现例如以下错误: 已将该虚拟机配置为使用 64 位客户机操作系统.可是,无法运行 64 位 ...
- 小白学react之网页获取微信用户信息
通过上一篇<小白学react之EJS模版实战>我们学习了怎样通过EJS模版生成我们高定制化的index.html文件. 本篇我们将会继续延续我们的alt-tutorial项目的实战计划.去 ...
- BP神经网络(手写数字识别)
1实验环境 实验环境:CPU i7-3770@3.40GHz,内存8G,windows10 64位操作系统 实现语言:python 实验数据:Mnist数据集 程序使用的数据库是mnist手写数字数据 ...
- zabbix通过snmp监控windows主机
1.开启Windows的snmp功能 2.配置snmp服务 设置snmp服务社区名称及允许的主机,设置完成后重启snmp服务 3.在zabbix server上测试 测试需要使用命令snmpwalk, ...