有没有办法知道 systemd 计时器下次运行的时间?

有没有办法知道 systemd 计时器下次运行的时间?

我正在测试 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有bug吗?

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

$ 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.

答案2

从@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

答案3

不,没有办法准确地查看计时器下次运行的时间。systemd提供systemctl list-timerssystemctl status something.timer,但这些并没有显示出影响AccuracySec=以及可能改变时间的其他指令。

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

有一个systemd 问题已解决使列表计时器的输出更准确/更少混乱。

此外,还有RandomizedDelaySec一个选项将AccuracySec根据手册页

答案4

要查看 的当前状态$SYSTEMD_TIMER,请运行

systemctl list-timers $SYSTEMD_TIMER

# will give output simmilar to
# NEXT                        LEFT         LAST PASSED UNIT           ACTIVATES
# Sun 2022-01-09 21:33:00 CET 2h 9min left n/a  n/a    $SYSTEMD_TIMER $SYSTEMD_SERVICE

因此,对于您的特定问题,命令是systemctl list-timers snapbackend.timer.

相关内容