如何重置用户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

并让我们知道。

相关内容