长期以来,多数Linux发行版一直在使用Unix System V引入的 init 系统。
init 由内核自身产生,任务是启动系统剩余部分,产生并监视所有其它进程,看其是否停止或僵死。
System V init 虽然过去一直运行的很好,但它已经有些迟暮了。
这也是 Ubuntu 为什么会使用Upstart来代替正在老化的 init 系统。
获取源代码地址: 从http://upstart.ubuntu.com/
linux启动过程如下图所示:
我们可以将整个过程划分为 3 个基本步骤,我将它们分别称之为 BIOS、内核引导 和 系统初始化
1. 通过init,添加新的守护进程
ubuntu 系统中 /etc/init/ 下会有很多 *.conf 文件. 如 acpid.conf.
xxha@xxha:/etc/init$ cat acpid.conf
# acpid - ACPI daemon
#
# The ACPI daemon provides a socket for other daemons to multiplex kernel
# ACPI events from, and a framework for reacting to those events.
description "ACPI daemon"
start on runlevel [2345]
stop on runlevel [!2345]
expect fork
respawn
exec acpid -c /etc/acpi/events -s /var/run/acpid.socket
这个acpid.conf文件,其实就是 acpid 进程的启动配置脚本。
$ ps aux |grep -v grep |grep acpid
root 1103 0.0 0.0 2172 708 ? Ss 19:58 0:00 acpid -c /etc/acpi/events -s /var/run/acpid.socket
这里 start on runlevel, 是指系统运行等级,这几个等级运行时,init 会运行该脚本中的进程。当这几个等级退出时,该进程会自动退出。
respawn 是指该进程结束时,重新开启该进程,也就时守护进程的意思。
这里有个bug, 系统会尝试几次(好象是5s钟,尝试10次),如果开启不了就会放弃。