1. 程式人生 > 實用技巧 >Ubuntu 開機啟動程式

Ubuntu 開機啟動程式

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 <[email protected]>"

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,開機啟動的規則好像又不一樣了