start on runlevel [2345] stop on shutdown respawn post-stop script # To avoid stomping on runsv's owned by a different runsvdir # process, kill any runsv process that has been orphaned, and is # now owned by init (process 1). pkill -HUP -P 1 runsv$ end script exec /opt/gitlab/embedded/bin/runsvdir-start
linux世界中init系统有许多种类,不同的发行版采用了不同的实现。大多数Linux发行版的init系统是和System V相兼容的,被称为"System V init(sysvinit)",这是人们最熟悉的init系统。早期Ubuntu也是使用的sysvinit,但是Ubuntu从6.10开始,开始用 Upstart 替换sysvinit,成为Ubuntu新一代init系统。现在也有一些linux发行版如Fedora、Debian也开始或者计划采用 systemd 来作为init系统。
(System V 是Unix众多版本中的一个分支,于1983年首次发布)
在2014年Debian项目决定在未来的版本中使用systemd后,马克·沙特尔沃思(Mark Richard Shuttleworth)宣布Ubuntu将开始计划将自身迁移到systemd,以保持与上游一致。但是到目前为止(ubuntu 14.10),ubuntu的默认的init系统还是Upstart,Upstart也兼容sysvinit,所以本文主要介绍"System V init"和Upstart这两种init系统。
# Boot-time system configuration/initialization script. si::sysinit:/etc/rc.sysinit
# What to do in single-user mode. ~:S:wait:/sbin/sulogin
# /etc/init.d executes the S and K scripts upon change # of runlevel. # # Runlevel 0 is halt. # Runlevel 1 is single-user. # Runlevels 2-5 are multi-user. # Runlevel 6 is reboot.
xxxx:/etc/init$ ls | xargs grep "startup" friendly-recovery.conf:emits startup friendly-recovery.conf: initctl emit startup hostname.conf:# This task is run on startup to set the system hostname from /etc/hostname, hostname.conf:start on startup kmod.conf:start on (startup mountall.conf:start on startup plymouth-ready.conf:start on startup or started plymouth-splash plymouth-upstart-bridge.conf:start on (startup udev-fallback-graphics.conf:# We only want this job to happen once per boot, hence 'startup and ...'. udev-fallback-graphics.conf:start on (startup and udev-finish.conf:start on (startup udevmonitor.conf:start on (startup udevtrigger.conf:start on (startup
作业hostname、kmod、mountall等都会监听startup信号(start on EVENT这个指令表示在EVENT发生时启动该程序)。Startup收集作业配置信息完成后,会发出"startup"信号,这些作业就会被执行了。
XXXX:/etc/init$ cat rc-sysinit.conf # rc-sysinit - System V initialisation compatibility # # This task runs the old System V-style system initialisation scripts, # and enters the default runlevel when finished.
description "System V initialisation compatibility" author "Scott James Remnant <scott@netsplit.com>"
start on (filesystem and static-network-up) or failsafe-boot stop on runlevel
# Default runlevel, this may be overriden on the kernel command-line # or by faking an old /etc/inittab entry env DEFAULT_RUNLEVEL=2
emits runlevel
# There can be no previous runlevel here, but there might be old # information in /var/run/utmp that we pick up, and we don't want # that. # # These override that env RUNLEVEL= env PREVLEVEL=
console output env INIT_VERBOSE
task
script # Check for default runlevel in /etc/inittab if [ -r /etc/inittab ] then eval "$(sed -nre 's/^[^#][^:]*:([0-6sS]):initdefault:.*/DEFAULT_RUNLEVEL="\1";/p' /etc/inittab || true)" fi
# Check kernel command-line for typical arguments for ARG in $(cat /proc/cmdline) do case "${ARG}" in -b|emergency) # Emergency shell [ -n "${FROM_SINGLE_USER_MODE}" ] || sulogin ;; [0123456sS]) # Override runlevel DEFAULT_RUNLEVEL="${ARG}" ;; -s|single) # Single user mode [ -n "${FROM_SINGLE_USER_MODE}" ] || DEFAULT_RUNLEVEL=S ;; esac done
# Run the system initialisation scripts [ -n "${FROM_SINGLE_USER_MODE}" ] || /etc/init.d/rcS
# Switch into the default runlevel telinit "${DEFAULT_RUNLEVEL}" end script
XXXX:/etc/init$ cat rc.conf # rc - System V runlevel compatibility # # This task runs the old System V-style rc script when changing between # runlevels.
description "System V runlevel compatibility" author "Scott James Remnant <scott@netsplit.com>"