10

I have unattended-upgrades installed on several servers, some version 12, some 14. I would like to enable MailOnlyOnError for them but would still like to receive email notices when an update requires reboot.

Will I still receive "reboot required" notices with MailOnlyOnError enabled?

Thanks!

azimut
  • 999
Whatsisname
  • 113
  • 1
  • 6

2 Answers2

10

Here is a solution based on an answer in this discussion. The trick is that the request for a reboot is indicated by the presence of the file /var/run/reboot-required.

Create a file /usr/local/sbin/email_update_required with the content

if [ -f /var/run/reboot-required ]; then
    echo "A reboot is required following updates to server `hostname`" | mail -s "Reboot Required" <mail-address>
fi

Adjust <mail-address> (for example to root or your personal email address). Check the permissions and make the file executable.

Put the following line into your root crontab (sudo crontab -e)

00 08 * * * /usr/local/sbin/email_update_required

(It runs the email_update_required script every day at 8 o’clock, adjust as needed.)

I've applied the above method to a Ubuntu 14.04 server with unattended-upgrades and MailOnlyOnError true", and it works.

Still, it would be nice to have a more "official" Ubuntu or unattended-upgrades way to get those notification mails. A switch similar to MailOnlyOnError would be great.

azimut
  • 999
0

Thanks to @Whatsisname and @azimut for the question and the solution. It is 2023 and I am using Ubuntu 22.04. While the solution still works, I have improved upon that script...

# To get ADMIN_EMAIL if defined
[ -f ~/.envrc ] && source ~/.envrc
email_address=${ADMIN_EMAIL:-"root@localhost"}

if [ -f /var/run/reboot-required ]; then echo "The server hostname will be rebooted, unattended, as per the schedule!"
| mail -s "Unattended Reboot" $email_address fi

Two points to note from the earlier answer by @azimut...

  • The script can be run as a normal user (as /var/run/reboot-required can be read by anyone)

  • Regarding the cron timing. This should fall between the time the server runs apt-daily-upgrade service and the Automatic-Reboot-Time defined in /etc/apt/apt.conf.d/50unattended-upgrades file. Otherwise, we will not be notified as the automatic reboot may have occurred already. The timing of apt-daily-upgrade service can be seen by running the command (as a normal user... systemctl status apt-daily-upgrade.timer). Automatic-Reboot-Time is user-defined. The default value is "now" that is usually not recommended when we want to alert ourselves with the upcoming automatic reboot.