Systemd

有沒有辦法知道下一個 systemd 計時器何時執行?

  • January 9, 2022

我正在測試一個 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

目前活動計時器的狀態可以使用以下命令顯示 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.

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

引用自:https://unix.stackexchange.com/questions/329996