如何重置使用者systemd計時器的剩餘時間

如何重置使用者systemd計時器的剩餘時間

如何重置使用者 systemd 計時器的剩餘時間?

我有一個用戶 systemd 服務:

[Unit]
Description=Remind to a take break

[Service]
ExecStart=/opt/scripts/break_reminder
Environment=PATH=/sbin:/bin:/usr/bin:/usr/sbin:/opt/scripts

[Install]
WantedBy=default.target

和一個用戶 systemd 計時器

[Unit]
Description=Remind to a take break

[Timer]
OnStartupSec=0min
OnUnitActiveSec=30min

[Install]
WantedBy=timers.target

編輯:透過重新啟動和停止+啟動來重新啟動計時器不會重置剩餘時間。

$ systemctl --user status break_reminder.timer 
● break_reminder.timer - Remind to take break
     Loaded: loaded (/home/adam/.config/systemd/user/break_reminder.timer; enabled; vendor pr>
     Active: active (waiting) since Sun 2021-05-30 16:19:00 CEST; 3h 48min ago
    Trigger: Sun 2021-05-30 20:24:05 CEST; 16min left
   Triggers: ● break_reminder.service

May 30 16:19:00 archadam systemd[383]: Started Remind to take break.

$ systemctl --user restart break_reminder.timer
$ systemctl --user status break_reminder.timer 
● break_reminder.timer - Remind to take break
     Loaded: loaded (/home/adam/.config/systemd/user/break_reminder.timer; enabled; vendor pr>
     Active: active (waiting) since Sun 2021-05-30 20:07:24 CEST; 1s ago
    Trigger: Sun 2021-05-30 20:24:05 CEST; 16min left
   Triggers: ● break_reminder.service

May 30 20:07:24 archadam systemd[383]: Stopping Remind to take break.
May 30 20:07:24 archadam systemd[383]: Started Remind to take break.

$ systemctl --user stop break_reminder.timer  
$ systemctl --user status break_reminder.timer
○ break_reminder.timer - Remind to take break
     Loaded: loaded (/home/adam/.config/systemd/user/break_reminder.timer; enabled; vendor pr>
     Active: inactive (dead) since Sun 2021-05-30 20:07:32 CEST; 1s ago
    Trigger: n/a
   Triggers: ● break_reminder.service

May 30 20:07:24 archadam systemd[383]: Stopping Remind to take break.
May 30 20:07:24 archadam systemd[383]: Started Remind to take break.
May 30 20:07:32 archadam systemd[383]: break_reminder.timer: Deactivated successfully.
May 30 20:07:32 archadam systemd[383]: Stopped Remind to take break.

$ systemctl --user start break_reminder.timer 
$ systemctl --user status break_reminder.timer
● break_reminder.timer - Remind to take break
     Loaded: loaded (/home/adam/.config/systemd/user/break_reminder.timer; enabled; vendor pr>
     Active: active (waiting) since Sun 2021-05-30 20:07:39 CEST; 2s ago
    Trigger: Sun 2021-05-30 20:24:05 CEST; 16min left
   Triggers: ● break_reminder.service

May 30 20:07:39 archadam systemd[383]: Started Remind to take break.

答案1

OnUnitActiveSec綁定到 的最後啟動時間break_reminder.service

OnUnitActiveSec=
定義與計時器單元正在啟動的單元上次啟動時間相關的計時器。

systemd.timer 線上說明頁

因此,要重設 的剩餘時間break_reminder.timer,您需要重設 的最後啟動時間戳記break_reminder.service

不幸的是,除了實際啟動服務單元之外,似乎沒有辦法重置此時間戳記。 (systemctl set-property無法更改時間戳,並且它既不受 影響systemctl reset-failedsystemctl clean也不受 影響systemctl daemon-reexec)。

答案2

重新啟動服務:

# systemctl restart break_reminder.timer

看看這是否有效。

如果不嘗試強制重啟:

# systemctl stop break_reminder.timer
# systemctl start break_reminder.timer

並讓我們知道。

相關內容