5

I am trying to understand how automatic upgrades are happening through the unattended-upgrades package. What is described below is what I found on a fresh install of Ubuntu 16.04.3.

/etc/cron.daily/apt-compat has a line exec /usr/lib/apt/apt.systemd.daily at the end which executes the script apt.systemd.daily.

systemd also executes /usr/lib/apt/apt.systemd.daily and it uses a timer. The systemd service definition doing the update can be found in /lib/systemd/system/apt-daily.service. It calls the script apt.systemd.daily with the argument update while the systemd service definition in /lib/systemd/system/apt-daily-upgrade.service calls the same script with the argument install.

As I understand, /usr/lib/apt/apt.systemd.daily is the script used by unattended-upgrades package to do the automatic upgrades. What I would like to know is why do both cron and systemd execute it?

5

The two jobs are complimentary, and the relationship is described in the cron job (16.04 and newer):

# Systemd systems use a systemd timer unit which is preferable to
# run. We want to randomize the apt update and unattended-upgrade
# runs as much as possible to avoid hitting the mirrors all at the
# same time. The systemd time is better at this than the fixed
# cron.daily time

On Ubuntu 16.04 and newer systems, the systemd timer is the important job. It does all the heavy lifting. The legacy cronjob merely triggers the systemd timer, in case for some reason it had been halted.

On Ubuntu 14.04 and older systems (non-Systemd), the system works quite differently: The cronjob triggers a run of unattended-updates.

| improve this answer | |
  • Can you please explain how the cronjob triggers the systemd timer? What I understood was that the cronjob triggers the /usr/lib/apt/apt.systemd.daily script directly. – Nilushan Costa Jan 17 '18 at 20:06
  • Well, it's pretty clear if you read the scripts. /usr/lib/apt/apt.systemd.daily checks that the systemd timer is working, and resets it if needed. Is there something specific you are looking for? – user535733 Jan 17 '18 at 20:58
  • I was trying to understand at what time the unattended upgrades are happening and was confused because I thought both the cronjob and systemd are executing it. But I think I understand it now. On 16.04 and newer versions, unattended upgrades are done through a systemd timer at a random time. When the cronjob is run, it only resets this timer if it has been halted. So the time unattended upgrades are run is decided by the systemd timer. Have I got it right? – Nilushan Costa Jan 18 '18 at 4:16
  • That is indeed accurate. Use systemctl list-timers to see when the next apt-daily and apt-daily-upgrade timers are scheduled. – user535733 Jan 18 '18 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.