尝试阻止 systemd 计时器触发错过的事件

尝试阻止 systemd 计时器触发错过的事件

尝试了此处发布的解决方案后(防止 systemd 计时器在启动时运行),我以为我的 systemd 计时器问题已得到纠正。然而,在我上次重新启动后,我的服务在启动期间启动(显然弥补了错过的事件)。

以下是有问题的文件:

btrfs_backup.timer

[Unit]
Description=Create mirror of current state of all BTRFS snapshots
Requires=btrfs_backup.service

[Timer]
# hourly, with a 5-minute delay, as to not interfere with the
# snapper-timeline.service.
Unit=btrfs_backup.service
OnCalendar=*-*-* *:05:00
Persistent=false

[Install]
WantedBy=timers.target

btrfs_backup.service

[Unit]
Description=Create mirror of current state of all BTRFS snapshots
Wants=btrfs_backup.timer

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/btrfs_backup
Environment="DISPLAY=:0.0"
Environment="XDG_RUNTIME_DIR=/run/user/0"

我基本上是从 snapper 的时间线 systemd 文件复制了这 (2) 个文件,所以我不明白为什么这个设置不起作用。我什至研究了从计时器中删除 [Install] 部分的建议,但是每一个我的系统(包括 snapper 系统)上的计时器有一个 [Install] 部分。

其他一切都运行良好 - 服务正确完成,通知显示在桌面上。

更新 #1:围绕问题事件的最后几个小时的日志条目:

Mar 01 14:05:09 dss-mint systemd[1]: Starting Create mirror of current state of all BTRFS snapshots...
Mar 01 14:05:09 dss-mint systemd[1]: btrfs_backup.service: Succeeded.
Mar 01 14:05:09 dss-mint systemd[1]: Finished Create mirror of current state of all BTRFS snapshots.
Mar 01 15:05:09 dss-mint systemd[1]: Starting Create mirror of current state of all BTRFS snapshots...
Mar 01 15:05:09 dss-mint systemd[1]: btrfs_backup.service: Succeeded.
Mar 01 15:05:09 dss-mint systemd[1]: Finished Create mirror of current state of all BTRFS snapshots.
Mar 01 16:05:09 dss-mint systemd[1]: Starting Create mirror of current state of all BTRFS snapshots...
Mar 01 16:05:09 dss-mint systemd[1]: btrfs_backup.service: Succeeded.
Mar 01 16:05:09 dss-mint systemd[1]: Finished Create mirror of current state of all BTRFS snapshots.
-- Reboot --
Mar 01 17:24:01 dss-mint systemd[1]: Starting Create mirror of current state of all BTRFS snapshots...
Mar 01 17:24:01 dss-mint systemd[1]: btrfs_backup.service: Succeeded.
Mar 01 17:24:01 dss-mint systemd[1]: Finished Create mirror of current state of all BTRFS snapshots.
Mar 01 18:05:19 dss-mint systemd[1]: Starting Create mirror of current state of all BTRFS snapshots...
Mar 01 18:05:19 dss-mint systemd[1]: btrfs_backup.service: Succeeded.
Mar 01 18:05:19 dss-mint systemd[1]: Finished Create mirror of current state of all BTRFS snapshots.

更新#2:好的,今天早上打开电脑。虽然服务在启动完成后不久就运行了,但计时器没有被触发:

$ ls-timers
NEXT                        LEFT          LAST                        PASSED       UNIT                         ACTIVATES                     
Tue 2021-03-02 08:43:16 PST 12min left    n/a                         n/a          systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Tue 2021-03-02 09:00:00 PST 29min left    n/a                         n/a          snapper-timeline.timer       snapper-timeline.service      
Tue 2021-03-02 09:01:57 PST 31min left    n/a                         n/a          btrfs_backup.timer           btrfs_backup.service          
Tue 2021-03-02 11:17:38 PST 2h 46min left Mon 2021-03-01 19:30:22 PST 13h ago      fwupd-refresh.timer          fwupd-refresh.service         
Tue 2021-03-02 16:53:23 PST 8h left       Mon 2021-03-01 12:52:17 PST 19h ago      motd-news.timer              motd-news.service             
Wed 2021-03-03 00:00:00 PST 15h left      Tue 2021-03-02 08:28:52 PST 1min 53s ago logrotate.timer              logrotate.service             
Wed 2021-03-03 00:00:00 PST 15h left      Tue 2021-03-02 08:28:52 PST 1min 53s ago man-db.timer                 man-db.service                
Sun 2021-03-07 03:10:21 PST 4 days left   Sun 2021-02-28 08:27:47 PST 2 days ago   e2scrub_all.timer            e2scrub_all.service           
Mon 2021-03-08 00:00:00 PST 5 days left   Mon 2021-03-01 08:33:26 PST 23h ago      fstrim.timer                 fstrim.service                
n/a                         n/a           Tue 2021-03-02 08:30:06 PST 39s ago      anacron.timer                anacron.service               
n/a                         n/a           Tue 2021-03-02 08:28:52 PST 1min 53s ago snapper-boot.timer           snapper-boot.service

我不明白这一点 - 我从未启用过该服务,因此它只会由计时器触发:

$ systemctl status btrfs_backup.service
● btrfs_backup.service - Create mirror of current state of all BTRFS snapshots
     Loaded: loaded (/etc/systemd/system/btrfs_backup.service; static; vendor preset: enabled)
     Active: inactive (dead) since Tue 2021-03-02 08:35:24 PST; 1min 8s ago
TriggeredBy: ● btrfs_backup.timer
    Process: 1206 ExecStart=/usr/local/sbin/btrfs_backup (code=exited, status=0/SUCCESS)
   Main PID: 1206 (code=exited, status=0/SUCCESS)

答案1

尝试Requires=btrfs_backup.service从计时器中取出。

systemd.unit(5) 手册页Requires=也会激活要求。因此激活定时器将激活 的要求btrfs_backup.service

相关内容