2

I want the apt-update timer to run on Sunday night between 4:30 and 5:30, and right after, the apt-upgrade timer. I have these settings:

cat << 'EOF' > /etc/systemd/system/apt-daily.timer
[Unit]
Description=Daily apt download activities

[Timer]
OnCalendar=Sun *-*-* 4:30:00
RandomizedDelaySec=60m
Persistent=true

[Install]
WantedBy=timers.target
EOF

cat << 'EOF' > /etc/systemd/system/apt-daily-upgrade.timer
[Unit]
Description=Daily apt upgrade and clean activities
After=apt-daily.timer

[Timer]
OnCalendar=Sun *-*-* 4:30:00
RandomizedDelaySec=60m
Persistent=true

[Install]
WantedBy=timers.target
EOF

systemctl daemon-reload

But sometimes this happens:

# systemctl list-timers
NEXT                         LEFT         LAST                         PASSED       UNIT                         ACTIVATES  
Sun 2022-01-16 04:43:35 CET  6 days left  Sun 2022-01-09 21:52:31 CET  7min ago     apt-daily-upgrade.timer      apt-daily-upgrade.service
Sun 2022-01-16 04:53:48 CET  6 days left  Sun 2022-01-09 21:52:31 CET  7min ago     apt-daily.timer              apt-daily.service

Systemd ignores the option After=apt-daily.timer. How can I fix this?

qq4
  • 282
  • 4
  • 14
Albert
  • 49
  • 1
  • 5

1 Answers1

0

A timer unit does not utilize the After or Before directives. A timer triggers a service unit when the timer has elapsed. A service can utilize the directives for ordering dependencies, so it is possible to have service files like this (simplified for explanation):

# apt-daily.service
[Unit]
Description=Daily apt download activities

[Service]
...
# apt-daily-upgrade.service
[Unit]
Description=Daily apt upgrade and clean activities
Requires=apt-daily.service
After=apt-daily.service

[Service]
...

Which could be used with a timer, such as:

# apt-daily.timer
[Unit]
Description=Daily apt download activities timer

[Timer]
OnCalendar=daily
Unit=apt-daily.service
qq4
  • 282
  • 4
  • 14
  • A related post that might offer some useful information https://unix.stackexchange.com/questions/216679/synchronize-systemd-unit-based-on-timers – qq4 Aug 14 '22 at 02:11
  • What exactly does "Requires" mean in the context of a timer? Once apt-daily.service has run at least once the requirement is fulfilled and next week apt-daily-upgrade.service runs before apt-daily.service? Same question for "After". – Graham Leggett May 23 '23 at 10:51
  • @GrahamLeggett those are service unit files, not timers. `Requires` indicates a dependency on another unit, and `After`/`Before` dictate ordering. I edited the answer to improve clarity between service and timer units. – qq4 May 24 '23 at 01:28