9

How can I write a script in Ubuntu such that it download updates at night when I sleep and reboots the computer if it needs a reboot?

16

You can do this by installing the unattended-upgrades package; see its documentation for details of its configuration.

sudo apt install unattended-upgrades

will install it for you.

Installing it should be sufficient to enable it. To allow it to reboot when necessary, you’ll have to add a configuration file, e.g. /etc/apt/apt.conf.d/52unattended-upgrades-local, containing

Unattended-Upgrade::Automatic-Reboot "yes";

By default, on Ubuntu, it will install any upgrade from the main repositories, whether it’s security-related or not. You can configure more repositories if necessary by overriding the Unattended-Upgrade::Allowed-Origins configuration key.

  • How official is this software source? I would not recommend to any user to install something from another source than their OS's software repository. Perhaps Ubuntu has something built-in (like CentOS's yum-cron)? – Ned64 Apr 7 at 19:12
  • 6
    @Ned64 it’s available as a package in Debian and Ubuntu, and is written by a Ubuntu developer. It piggy-backs off hooks in apt itself. – Stephen Kitt Apr 7 at 19:15
  • What do you suppose this package does if the file /var/run/reboot-required is never generated by the system? That is to say, some systems generate this file, others do not. – Seamus Apr 7 at 20:17
  • 1
    @Seamus are there Debian or Ubuntu derivatives where reboot-required isn’t generated? – Stephen Kitt Apr 7 at 20:20
  • The only one I'm certain of is Raspbian (Raspberry Pi's default OS). Just out of curiosity - Isn't Ubuntu an "offspring" of Debian? – Seamus Apr 7 at 21:14

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.