我的 systemd 计时器配置如下,应每 5 分钟执行一次服务。
[Unit]
Description=Sync mail
[Timer]
OnCalendar=*:0/5
Unit=mail.service
[Install]
WantedBy=multi-user.target
然而,定时器状态是:
● mail.timer - Sync mail
Loaded: loaded (/home/phil/.config/systemd/user/mail.timer; enabled; vendor preset: enabled)
Active: active (running) since Sat 2021-06-12 09:49:05 CEST; 15min ago
Trigger: n/a
Triggers: ● mail.service
Jun 12 09:49:05 arch systemd[396]: Started Sync mail.
只触发过一次,之后就不再触发了。有什么建议吗?为什么会这样?
答案1
计时器会说Trigger: n/a
只要它触发的单元仍在运行。它不会尝试安排已在运行的服务。当服务停止时,计时器触发器将设置为下一个适当的时间。
如果您的 mail.service 需要 12 分钟才能执行,计时器将触发一次,该服务将执行 12 分钟,然后计时器将在 3 分钟后安排它,从而导致两次调用之间有 15 分钟的间隔。
所以,答案是“要有耐心”。完成后mail.service
,它将在下一个 5 分钟时间点再次开始。
这是一个例子:
# mytime.service
[Service]
Type=oneshot
ExecStart=/bin/sleep 10
# mytime.timer
[Timer]
OnCalendar=*:0/1
当我systemctl start mytime.timer
我们会看到这个:
$ systemctl --user status mytime.timer mytime.service
● mytime.timer - Test timer
Active: active (waiting) since Sat 2021-06-12 10:56:01 CEST; 725ms ago
Trigger: Sat 2021-06-12 10:57:00 CEST; 59s left
● mytime.service
Active: inactive (dead)
当服务执行时,我们将看到:
$ systemctl --user status mytime.timer mytime.service
● mytime.timer - Test timer
Active: active (running) since Sat 2021-06-12 10:56:01 CEST; 1min ago
Trigger: n/a
● mytime.service
Active: activating (start) since Sat 2021-06-12 10:57:00 CEST; 10ms ago
CGroup: /user.slice/user-1000.slice/[email protected]/app.slice/mytime.service
└─11346 /bin/sleep 10
一旦sleep 10
退出,触发器将被设置:
$ systemctl --user status mytime.timer mytime.service
● mytime.timer - Test timer
Active: active (waiting) since Sat 2021-06-12 10:56:01 CEST; 1min 11s ago
Trigger: Sat 2021-06-12 10:58:00 CEST; 49s left
● mytime.service
Active: inactive (dead) since Sat 2021-06-12 10:57:10 CEST; 1s ago
Process: 11346 ExecStart=/bin/sleep 10 (code=exited, status=0/SUCCESS)