Systemd

ExecStart 有一個“-”前綴,但單元仍然失敗

  • July 4, 2022

我已經覆蓋了 dnf-makecache 不會讓我的系統在每次互聯​​網上出現問題時都處於失敗狀態(令人震驚!),但它似乎被忽略了。

# sc cat dnf-makecache.service
# /usr/lib/systemd/system/dnf-makecache.service
[Unit]
Description=dnf makecache
# On systems managed by either rpm-ostree/ostree, dnf is read-only;
# while someone might theoretically want the cache updated, in practice
# anyone who wants that could override this via a file in /etc.
ConditionPathExists=!/run/ostree-booted

After=network-online.target

[Service]
Type=oneshot
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
Environment="ABRT_IGNORE_PYTHON=1"
ExecStart=/usr/bin/dnf makecache --timer

# /etc/systemd/system/dnf-makecache.service.d/override.conf
[Service]
ExecStart=-/usr/bin/dnf makecache --timer
[root@lxd10 ~]# man systemd.service
[root@lxd10 ~]# sc cat dnf-makecache
# /usr/lib/systemd/system/dnf-makecache.service
[Unit]
Description=dnf makecache
# On systems managed by either rpm-ostree/ostree, dnf is read-only;
# while someone might theoretically want the cache updated, in practice
# anyone who wants that could override this via a file in /etc.
ConditionPathExists=!/run/ostree-booted

After=network-online.target

[Service]
Type=oneshot
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
Environment="ABRT_IGNORE_PYTHON=1"
ExecStart=/usr/bin/dnf makecache --timer

# /etc/systemd/system/dnf-makecache.service.d/override.conf
[Service]
ExecStart=-/usr/bin/dnf makecache --timer

但它仍然失敗並且系統狀態已降級

Jun 28 09:08:35 lxd10.2e-systems.com systemd[1]: dnf-makecache.service: Main process exited, code=exited, status=1/FAILURE
Jun 28 09:08:35 lxd10.2e-systems.com systemd[1]: dnf-makecache.service: Failed with result 'exit-code'.
Jun 28 09:08:35 lxd10.2e-systems.com systemd[1]: Failed to start dnf makecache.

但手冊明確指出,如果 exec 路徑以破折號為前綴,則退出程式碼將被忽略。我究竟做錯了什麼?也許是因為它是由計時器執行的?

來自systemd.service(5)

除非Type=is oneshot,否則必須給出一個命令。**使用時 Type=oneshot,可以指定零個或多個命令。**可以通過在同一指令中提供多個命令行來指定命令,或者,可以多次指定該指令以具有相同的效果。如果將空字元串分配給此選項,則要啟動的命令列表將被重置,此選項的先前分配將無效。

$$ … $$

同樣,從systemd.unit(5)

請注意,對於插入文件,如果想要從被解析為列表(並且不是依賴項)的設置中刪除條目,例如 AssertPathExists=(或例如**ExecStart=在服務單元中**),則需要 先清除列表,然後再重新- 添加除要刪除的條目之外的所有條目。

$$ … $$

因此,使用目前 OP 的override.conf文件,因為類型是oneshotExecStart=現在是一個兩元素列表:/usr/bin/dnf makecache --timer-/usr/bin/dnf makecache --timer. 適用於:

如果指定了多個命令,則這些命令按照它們在單元文件中出現的順序依次呼叫。如果其中一個命令失敗(並且沒有以“-”為前綴),則不會執行其他行,並且該單元被視為失敗。

由於-仍然執行原始命令 without ,當它失敗時,服務失敗。

替換它,該override.conf文件應包含如下內容:

[Service]
ExecStart=
ExecStart=-/usr/bin/dnf makecache --timer

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