OnCalendar systemd 定时器单元仍然在启动时执行,如何停止它?

OnCalendar systemd 定时器单元仍然在启动时执行,如何停止它?

我启用并启动了以下单元

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

[Install]
WantedBy=timers.target

它会在每晚凌晨 3.15 正确运行,但它也会在启动时运行,当它造成混乱时!为什么会发生这种情况以及如何阻止它?

答案1

这得到了很好的回答这个答案防止 systemd 计时器在启动时运行

我来总结一下

问题是我总是在 .service 文件的 [Install] 部分中包含类似 WantedBy=basic.target 的内容(因为它是标准 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

太长了;您不希望 .service 文件中的 [Install] 部分由 .timer 文件触发。

作为附加步骤你必须禁用你的服务systemctl 禁用 [服务名称]

然后你会注意到,如果你想再次启用它,你不能,并且会显示类似的错误:

请注意说的部分

...在需要时通过激活启动(套接字、路径、计时器...

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.

答案2

我也有同样的麻烦。每次启动时都会启动 OnCalendar 上绑定的计时器。定时器:

[Unit]
Description=Samba backup timer
#Requires=samba-backup.service

[Timer]
OnCalendar=*-*-* 01:00:00
Persistent=true
AccuracySec=1s
Unit=samba-backup.service

[Install]
WantedBy=timers.target

服务:

[Unit]
Description=Samba backup service

[Service]
#User=root
#Group=root
Type=oneshot
ExecStart=/usr/local/sbin/samba-backup.sh
StandardOutput=append:/var/log/samba-backup.log
StandardError=append:/var/log/samba-backup-err.log

在我评论后,计时器和服务单元被放置在 /etc/systemd/system 中#Requires=samba-backup.service 一切正常,计时器仅根据调度程序时间启动。

相关内容