Systemd 管理的资源的单位是 unit,最常见的 unit 就是一个服务。我们经常使用 systemctl status 查看某些服务的状态,来确认这个服务的运行是否正常。像下面这样:

➜ ~ $ systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2018-09-13 11:53:33 CST; 1 day 2h ago
     Docs: https://docs.docker.com
Main PID: 1917 (dockerd)

这里的 LoadedActive 两行就表示这个 unit 的状态,其中 Loaded 表示这个 unit 是否已经正确读到内存之中,Active 则表示这个 unit 的运行状态。另外,Active 这里包含了一个 state 和一个 substate,括号内的是 substate,state 的状态定义对所有 unit 都是一样的,但是不同类型的 unit 会定义不同的 substate (systemd 有 11 种类型的 unit,具体请查看 man page)。

那么一个 unit 到底有哪些状态呢?我发现我在 man page 中找不到所有状态的说明,但是有个神奇的命令却能列出所有的状态:

systemctl --state=help

Available unit load states:
stub
loaded
not-found
error
merged
masked

Available unit active states:
active
reloading
inactive
failed
activating
deactivating

Available automount unit substates:
dead
waiting
running
failed

Available device unit substates:
dead
tentative
plugged

Available mount unit substates:
dead
mounting
mounting-done
mounted
remounting
unmounting
remounting-sigterm
remounting-sigkill
unmounting-sigterm
unmounting-sigkill
failed

Available path unit substates:
dead
waiting
running
failed

Available scope unit substates:
dead
running
abandoned
stop-sigterm
stop-sigkill
failed

Available service unit substates:
dead
start-pre
start
start-post
running
exited
reload
stop
stop-sigabrt
stop-sigterm
stop-sigkill
stop-post
final-sigterm
final-sigkill
failed
auto-restart

Available slice unit substates:
dead
active

Available socket unit substates:
dead
start-pre
start-chown
start-post
listening
running
stop-pre
stop-pre-sigterm
stop-pre-sigkill
stop-post
final-sigterm
final-sigkill
failed

Available swap unit substates:
dead
activating
activating-done
active
deactivating
deactivating-sigterm
deactivating-sigkill
failed

Available target unit substates:
dead
active

Available timer unit substates:
dead
waiting
running
elapsed
failed

知识共享许可协议本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。