有没有办法知道什么时候会运行systemd计时器?


19

我正在测试systemd计时器,并尝试覆盖其默认超时,但是没有成功。我想知道是否有一种方法可以要求systemd告诉我们该服务何时将在下一次运行。

普通文件(/lib/systemd/system/snapbackend.timer):

# Documentation available at:
# https://www.freedesktop.org/software/systemd/man/systemd.timer.html

[Unit]
Description=Run the snapbackend service once every 5 minutes.

[Timer]
# You must have an OnBootSec (or OnStartupSec) otherwise it does not auto-start
OnBootSec=5min
OnUnitActiveSec=5min
# The default accuracy is 1 minute. I'm not too sure that either way
# will affect us. I am thinking that since our computers will be
# permanently running, it probably won't be that inaccurate anyway.
# See also:
# http://stackoverflow.com/questions/39176514/is-it-correct-that-systemd-timer-accuracysec-parameter-make-the-ticks-slip
#AccuracySec=1

[Install]
WantedBy=timers.target

# vim: syntax=dosini

替代文件(/etc/systemd/system/snapbackend.timer.d/override.conf):

# This file was auto-generated by snapmanager.cgi
# Feel free to do additional modifications here as
# snapmanager.cgi will be aware of them as expected.
[Timer]
OnUnitActiveSec=30min

我运行了以下命令,计时器仍然每5分钟滴答一次。systemd中可能有错误吗?

sudo systemctl stop snapbackend.timer
sudo systemctl daemon-reload
sudo systemctl start snapbackend.timer

所以我也想知道,我怎么知道下一个计时器何时开始计时?因为那会立即告诉我是否在5分钟内。或30分钟 但从那systemctl status snapbackend.timer说不出什么。只是想知道是否有命令可以告诉我当前使用的延迟。

对于那些感兴趣的人,也有服务文件(/lib/systemd/system/snapbackend.service),尽管我想这对计时器的滴答声应该没有影响...

# Documentation available at:
# https://www.freedesktop.org/software/systemd/man/systemd.service.html

[Unit]
Description=Snap! Websites snapbackend CRON daemon
After=snapbase.service snapcommunicator.service snapfirewall.service snaplock.service snapdbproxy.service

[Service]
# See also the snapbackend.timer file
Type=simple
WorkingDirectory=~
ProtectHome=true
NoNewPrivileges=true
ExecStart=/usr/bin/snapbackend
ExecStop=/usr/bin/snapstop --timeout 300 $MAINPID
User=snapwebsites
Group=snapwebsites
# No auto-restart, we use the timer to start once in a while
# We also want to make systemd think that exit(1) is fine
SuccessExitStatus=1
Nice=5
LimitNPROC=1000
# For developers and administrators to get console output
#StandardOutput=tty
#StandardError=tty
#TTYPath=/dev/console
# Enter a size to get a core dump in case of a crash
#LimitCORE=10G

[Install]
WantedBy=multi-user.target

# vim: syntax=dosini

1
systemctl list-timers帮助输出吗?
phg

啊! 通过搜索,我找到了包含解决方案的页面:bbs.archlinux.org/viewtopic.php?id=214989我现在要写一个答案。
亚历克西斯·威尔克

Answers:


25

可以使用systemctl list-timers以下命令显示当前活动计时器的状态 :

$ systemctl list-timers --all
NEXT                         LEFT     LAST                         PASSED       UNIT                         ACTIVATES
Wed 2016-12-14 08:06:15 CET  21h left Tue 2016-12-13 08:06:15 CET  2h 18min ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service

1 timers listed.

7

通过@phg注释和答案,我找到了包含答案的页面。计时器是累积的,您需要首先将其重置,否则先前的条目将保留下来。这对日历很有用,但对所有计时器都相同。

具有一个可以在将计时器更改为新值之前将其重置的条目可以按预期工作:

# This file was auto-generated by snapmanager.cgi
# Feel free to do additional modifications here as
# snapmanager.cgi will be aware of them as expected.
[Timer]
OnUnitActiveSec=
OnUnitActiveSec=30min

1

不,没有一种方法可以确切地看到计时器何时下次运行。systemd提供systemctl list-timerssystemctl status something.timer,但这些没有显示的影响,AccuracySec=也可能没有显示其他会改变时间的指令。

如果您AccuracySec=1h在两台服务器上进行设置,则它们都将报告两台服务器上的同一计时器将在完全相同的时间触发,但实际上它们之间的启动时间可能长达一个小时!如果您想知道两个随机计时器是否可能发生冲突,似乎无法检查最终计算出的运行时间以找出答案。

有一个系统问题尚未解决,它使列表计时器的输出更加准确/减少了混乱。


关于计时器的有趣观点。list-timers但是,我们从中获得的信息对于了解您对计时器的使用是否正确已经非常有用。
Alexis Wilke

1
就我而言不是。我想在双主机上使用完全相同的配置,但是要使用AccuracySec =来确保两者没有同时进行维护。我想看看计时器何时会在每个主机上实际触发,但不能。
Mark Stosberg

啊。我有类似的问题。我将使用一个选定的主机(使用投票系统),然后主机向计算机1发送消息“进行维护”,一旦计算机1完成,它将向主机报告其新状态,然后主机2要求计算机2进行维护,这些计算机之一当然是主计算机,但是运行维护循环的代码应与实际维护分开。要记住的一个问题。如果群集是增长了不少记住,这需要时间,这可能是这么久,有些计算机没有得到很长一段时间更新了!
Alexis Wilke
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.