Albert
Albert

Reputation: 69

Execute systemd service just after another using a timer

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?

Upvotes: 4

Views: 2051

Answers (1)

qq4
qq4

Reputation: 316

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

Upvotes: 3

Related Questions