Linux

OnCalendar systemd 計時器單元仍在啟動時執行,如何停止它?

  • December 8, 2021

我啟用並啟動了以下單元

[Unit]
Description=Schedule a nightly execution at 03.15 for Backup ROOT
# Allow manual start
RefuseManualStart=no
# Allow manual stop
RefuseManualStop=no

[Timer]
#Execute job if it missed a run due to machine being off
Persistent=false
# Run every night 03.15
OnCalendar=*-*-* 03:15:00
#File describing job to execute
Unit=schedule-backup@root.service

[Install]
WantedBy=timers.target

它會在每晚凌晨 3.15 點正確執行,但它也會在啟動時執行,因為它會造成混亂!為什麼會發生這種情況以及如何阻止它?

防止 systemd timer from running on startup的這個答案很好地回答了這個 問題。

我會總結

問題是我總是在

$$ Install $$.service 文件的部分(因為它是標準 systemd 服務複製麵食的一部分)。事實證明,這實際上會導致該單元在 basic.target 為(又名系統啟動)時啟動。 https://www.freedesktop.org/software/systemd/man/systemd.unit.html#WantedBy= https://www.freedesktop.org/software/systemd/man/systemd.special.html#basic.target

TLDR;你不想要一個

$$ Install $$由 .timer 文件觸發的 .service 文件中的部分。

作為附加步驟,您必須使用systemctl disable禁用您的服務$$ service_name $$

然後你會注意到,如果你想再次啟用它,你不能並且會顯示一個類似的錯誤:

注意說的部分

…在需要時通過啟動(套接字、路徑、計時器、…

The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
Alias= settings in the [Install] section, and DefaultInstance= for template
units). This means they are not meant to be enabled using systemctl.

   Possible reasons for having this kind of units are:

• A unit may be statically enabled by being symlinked from another unit's
 .wants/ or .requires/ directory.
• A unit's purpose may be to act as a helper for some other unit which has
 a requirement dependency on it.
• A unit may be started when needed via activation (socket, path, timer,
 D-Bus, udev, scripted systemctl call, ...).
• In case of template units, the unit is meant to be enabled with some
 instance name specified.

引用自:https://unix.stackexchange.com/questions/680669