init的进程号为1,是所有进程的父进程,内核初始化完毕之后,init程序开始运行。其他软件也同时开始运行。init程序通过/etc/inittab文件进行配置。
/etc/inittab文件格式:id:run-levels:action:process,共包含4项,用冒号分隔,其中某些部份可以为空,各项详细解释如下:
id
标识符,一般为两位字母或数字,该标识符唯一,在配置文件中不能重复。
run-level
指定系统运行级,即执行登记项的init级别。用于指定相应的登记项适用于哪一个运行级,即在哪一个运行级中被处理。如果该字段为空,那么相应的登记项将适用于所有的运行级。在该字段中,可以同时指定一个或多个运行级,其中各运行级分别以数字0 1 2 3 4 5 6或字母a、b、c 表示,且无需对其进行分隔。
Linux有7个运行级,如下:
0-halt 关机,让init关闭所有进程并关机
1-Single user mode 单用户字符界面,通常又称为s或S
2-Multiuser,without NFS 不具备网络文件系统功能的多用户字符界面
3-Full multiuser mode 具备网络文件系统功能的多用户字符界面
4-unused 保留不用
5-X11 具备网络功能的图形用户界面
6-reboot 关闭所有运行的进程并重新启动系统
除此之外还有ABC三个运行级别,但在Linux都没有意义。
action
用于指定<process>可能用到的动作
process
指定要运行的Shell脚本/命令。
/etc/inittab
# /etc/inittab
#
# Copyright (C) 2001 Erik Andersen <andersen@codepoet.org>
#
# Note: BusyBox init doesn't support runlevels. The runlevels field is
# completely ignored by BusyBox init. If you want runlevels, use
# sysvinit.
#
# Format for each entry: <id>:<runlevels>:<action>:<process>
#
# id == tty to run on, or empty for /dev/console
# runlevels == ignored
# action == one of sysinit, respawn, askfirst, wait, and once
# process == program to run
# Startup the system
::sysinit:/bin/mount -t proc proc /proc
::sysinit:/bin/mount -o remount,rw /
::sysinit:/bin/mkdir -p /dev/pts /dev/shm
::sysinit:/bin/mount -a
::sysinit:/sbin/swapon -a
null::sysinit:/bin/ln -sf /proc/self/fd /dev/fd
null::sysinit:/bin/ln -sf /proc/self/fd/0 /dev/stdin
null::sysinit:/bin/ln -sf /proc/self/fd/1 /dev/stdout
null::sysinit:/bin/ln -sf /proc/self/fd/2 /dev/stderr
::sysinit:/bin/hostname -F /etc/hostname
# now run any rc scripts
::sysinit:/etc/init.d/rcS
# Put a getty on the serial port
#ttyS0::respawn:/sbin/getty -L ttyS0 115200 vt100 # GENERIC_SERIAL
# Stuff to do for the 3-finger salute
#::ctrlaltdel:/sbin/reboot
# Stuff to do before rebooting
::shutdown:/etc/init.d/rcK
::shutdown:/sbin/swapoff -a
::shutdown:/bin/umount -a -r
/etc/init.d/
/etc/init.d/rcS
#!/bin/sh
# Start all init scripts in /etc/init.d
# executing them in numerical order.
#
for i in /etc/init.d/S??* ;do
# Ignore dangling symlinks (if any).
[ ! -f "$i" ] && continue
case "$i" in
*.sh)
# Source shell script for speed.
(
trap - INT QUIT TSTP
set start
. $i
)
;;
*)
# No sh extension, so fork subprocess.
$i start
;;
esac
done
/etc/init.d/rcK
#!/bin/sh
# Stop all init scripts in /etc/init.d
# executing them in reversed numerical order.
#
for i in $(ls -r /etc/init.d/S??*) ;do
# Ignore dangling symlinks (if any).
[ ! -f "$i" ] && continue
case "$i" in
*.sh)
# Source shell script for speed.
(
trap - INT QUIT TSTP
set stop
. $i
)
;;
*)
# No sh extension, so fork subprocess.
$i stop
;;
esac
done
trap - INT QUIT TSTP
忽略这三个信号,防止脚本执行时使用ctrl-C 就退出脚本
总结:
开机初始化的时候执行/etc/init.d/rcS脚本,该脚本会按Sxx的顺序执行/etc/init.d/下的所有脚本,传参start 开启相对应的服务。
关机的时候执行/etc/init.d/rcK脚本,该脚本会按Sxx的顺序执行/etc/init.d/下的所有脚本,传参stop 停止相对应的服务。
rc是run command S是Start K 是Kill
有些时候在/etc/init.d/目录下有一个rc脚本文件 在rcS中执行rc 传参S,启动/etc/rcS.d下的所有脚本,实际/etc/rcS.d下的所有脚本文件都是软链接到/etc/init.d/目录下
在rcS中执行rc 传参N(N代表的是run-level 0、1、2、3、4、5、6),启动/etc/rcN.d下的所有脚本,其中run-level1或者S代表单用户模式
总之etc/init.d/rcS将会执行etc/init.d/下面所有的脚本,脚本名字一般命名以Sxx开头,其中xx代表脚本执行的顺序,如果需要开机的时候开启某个服务,那么在etc/init.d/目录下增加Sxx脚本,开机进度条的显示就是根据Sxx的脚本数量步进的,每执行一个Sxx脚本,进度条步进一格,所有脚本执行完毕,进度条走满。
以.sh结尾的脚本是必须执行的脚本,不是以.sh结尾的脚本服务是可以开启或关闭的,通过start、stop、restart、pause、zap、status、ineed、iuse、needsme、usesme或者broken参数