Systemd

如何刪除失去的 systemd 單元?

  • January 11, 2020

我無法弄清楚如何刪除不再有文件的 systemd 單元。他們似乎仍然以某種方式徘徊在系統中。

我要刪除的舊損壞單元:

core@ip-172-16-32-83 ~ $ systemctl list-units --all firehose-router*
 UNIT                       LOAD      ACTIVE SUB    DESCRIPTION
<E2><97><8F> firehose-router@02.service not-found failed failed firehose-router@02.service
<E2><97><8F> firehose-router@03.service not-found failed failed firehose-router@03.service

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

2 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.

這些文件不存在,但重新載入仍然有這些單位揮之不去:

core@ip-172-16-32-83 ~ $ systemctl list-unit-files firehose-router@02.service
core@ip-172-16-32-83 ~ $ sudo systemctl daemon-reload
core@ip-172-16-32-83 ~ $ systemctl list-units --all firehose-router*
 UNIT                       LOAD      ACTIVE SUB    DESCRIPTION
<E2><97><8F> firehose-router@02.service not-found failed failed firehose-router@02.service
<E2><97><8F> firehose-router@03.service not-found failed failed firehose-router@03.service

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

2 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.

我找不到與它們相關的文件:

core@ip-172-16-32-83 ~ $ sudo find /var/run/systemd -name "*firehose-router*"
core@ip-172-16-32-83 ~ $ find /etc/systemd/ -name "*firehose-router*"
core@ip-172-16-32-83 ~ $ find /usr/lib/systemd/ -name "*firehose-router*"
core@ip-172-16-32-83 ~ $

那麼我該如何擺脫這些呢?

您要執行的命令是systemctl reset-failed

當 systemd 分析單元定義文件時,它會記錄文件中呼叫的任何其他相關單元——無論這些其他單元是否存在。

$ systemctl --state=not-found --all
> ( ...prints list of 'not-found' units )

$ grep -r "<missing-unit>" /usr/lib/systemd/system
> ( returns files with references to <missing-unit> )

當一個單元顯示為“未找到”時,它不一定是錯誤 - 我們所知道的是,本地單元定義聲稱與它有某種關係。這種關係可能不是我們關心的。例如,它可能是"Before:"某個其他單位,但我們不使用該其他單位。

引用自:https://serverfault.com/questions/606520