我有以下 oneshot 服务+计时器:
$ systemctl cat stop_syncro.service stop_syncro.timer
# /usr/local/lib/systemd/system/stop_syncro.service
[Unit]
Description=Service d_arret de la Synchro
After=network-online.target
[Service]
Type=oneshot
User=root
ExecStart=/root/shl/RUN/stop_syncro.sh
[Install]
WantedBy=multi-user.target
# /usr/local/lib/systemd/system/stop_syncro.timer
[Unit]
Description=Timer d_arret de la Synchro
[Timer]
OnCalendar=Mon-Fri *-*-* 8:00:00
[Install]
WantedBy=timers.target
然后我重新启动了服务器:
$ last reboot | head -1
reboot system boot 3.10.0-1160.71.1 Tue May 30 17:53 - 15:02 (21:09)
并且服务似乎在重启后立即运行
$ journalctl -u stop_syncro.service
-- Logs begin at Tue 2023-05-30 17:53:15 CEST, end at Wed 2023-05-31 14:48:17 CEST. --
May 30 17:53:26 PAR-SCAL-01 systemd[1]: Starting Service d_arret de la Synchro...
May 30 17:53:27 PAR-SCAL-01 systemd[1]: Started Service d_arret de la Synchro...
May 31 08:00:01 PAR-SCAL-01 systemd[1]: Starting Service d_arret de la Synchro...
May 31 08:00:02 PAR-SCAL-01 systemd[1]: Started Service d_arret de la Synchro...
$
如何防止我的 oneshot 服务仅在早上 8 点触发,而不是在重启后启动?
勿
[Install]
服入multi-user.target
。我强烈怀疑它不是由计时器运行的——它是在启动时运行的,因为你要求它在启动时运行,方法是将它放在 multi-user.target 中。因此
systemctl disable
,.service 可能会删除其 [Install] 部分,以防止将来意外重新启用它。