9

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
  • 949
  • 2
  • 12
  • 27
Whatsisname
  • 103
  • 5
  • Good question. I have a server with unnattended-upgrades and MailOnlyOnError and my impression is that unfortunately I don't get any info mails if a reboot is required. Would be nice to know if there is an "official" way to make this work. – azimut Oct 24 '14 at 10:39

2 Answers2

9

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
  • 949
  • 2
  • 12
  • 27
  • Thanks! The discussion you linked to also points to [this one](http://serverfault.com/questions/92932/how-does-ubuntu-keep-track-of-the-system-restart-required-flag-in-motd/92940#92940) which pointed me at using nagios to monitor /var/run/reboot. That'll work for us. – Whatsisname Oct 27 '14 at 15:47
  • Ah, wasn't aware of that other discussion. Thanks. – azimut Oct 27 '14 at 15:49
  • When posting this answer, I had applied the same to a server with unattended updates. In the last unatteded update, linux-image-3.13.0-39-generic was installed. And I got the first reboot-required message, so it works! – azimut Oct 29 '14 at 07:52
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.