Ubuntu 的開機啟動項都放在 /etc/init 下的 *.conf 文件
以 /etc/init/nginx.conf 為例子
description "nginx - small, powerful, scalable web/proxy server"
start on filesystem and static-network-up ## 在文件系統和網絡啟動后啟動 nginx
stop on runlevel [016] ## 在 level 0, 1, 6 的時候停止 nginx
expect fork ## 監控 fork 一次后的 pid
## expect daemonize 則是監控 fork 兩次后的 pid (有的 daemon 程序會 fork 兩次)
respawn ## 如果 nginx 失敗了會自動重啟
pre-start script
## pre-start 步驟,檢查 nginx 是否存在,通過 -q -t 檢查 nginx 配置
[ -x /usr/sbin/nginx ] || { stop; exit 0; }
/usr/sbin/nginx -q -t -g 'daemon on; master_process on;' || { stop; exit 0; }
end script
## 啟動 nginx 的命令,可以有多個步驟放在 script...end script 內
exec /usr/sbin/nginx -g 'daemon on; master_process on;'
## 停止 nginx 的命令
pre-stop exec /usr/sbin/nginx -s quit
其中有一個特殊的文件 rc.conf
這是為了兼容早期的版本,早期的啟動配置放在了 /etc/rc[runlevel].d 或者 /etc/rc.d/rc[runlevel].d
# 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>"
emits deconfiguring-networking
emits unmounted-remote-filesystems
start on runlevel [0123456]
stop on runlevel [!$RUNLEVEL]
export RUNLEVEL
export PREVLEVEL
console output
env INIT_VERBOSE
task
script
if [ "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "1" -o "$RUNLEVEL" = "6" ]; then
status plymouth-shutdown 2>/dev/null >/dev/null && start wait-for-state WAITER=rc WAIT_FOR=plymouth-shutdown || :
fi
/etc/init.d/rc $RUNLEVEL
end script
可以看到實際上是啟動了 /etc/init.d/rc RUNLEVEL 命令
rc 命令會調用 /etc/rc$runlevel.d/ 下的程序,這些程序以 [S|K][level][name] 的規則命名
其中 S 代表啟動,K 代表停止,level 代表執行順序,比如
/etc/rc2.d/S02ssh
/etc/rc2.d/S03docker
這表示當 RUN LEVEL 是 2 的時候,先啟動 ssh 再啟動 docker
進一步查看這些腳本發現都是鏈接
S02ssh -> ../init.d/ssh
S03docker -> ../init.d/docker
實際上 rc 腳本的做法是遇到 S 腳本就傳個 start,遇到 K 腳本就傳個 stop,並按 level 的順序執行
相當於執行
/etc/init.d/ssh start
/etc/init.d/docker start
不同系統的 run level 的含義不一樣
ubuntu 默認 run level 是 2,各 leve 含義是
0 - Halt
1 - Single
2 - Full multi-user with display manager (GUI)
3 - Full multi-user with display manager (GUI)
4 - Full multi-user with display manager (GUI)
5 - Full multi-user with display manager (GUI)
6 - Reboot
而 RedHat 的 run level 含義是
0 - Halt
1 - Single
2 - Not used/User definable
3 - Full multi-user NO display manager
4 - Not used/User definable
5 - Full multi-user with display manager (GUI)
6 - Reboot
還有一個 /etc/rcS.d 目錄,應該是有一個 run level 是 S
另外基本上每個 /etc/rc$runlevel.d/ 下都有像這樣的命令
S05rc.local -> ../init.d/rc.local
而 /etc/init.d/rc.local 腳本會調用 /etc/rc.local 或 /etc/rc.d/rc.local
而 /etc/rc.local 是空的,可以往里面添加命令,這樣就可以在開機時執行
添加開機啟動項有以下方法
- 直接添加到 rc.local
- 建立 /etc/init.d/xxx 鏈接,然后再建立鏈接如 /etc/rc2.d/S99xxx -> ../init.d/xxx
- 建立 /etc/init.d/xxx 鏈接,然后用命令 sudo update-rc.d xxx defaults 99 創建
在 Ubuntu 18.04,開機啟動的規則好像又不一樣了