Unattended upgrades: Reboot Required E-mail Notification

I like my systems to automatically apply security updates. Sometimes these require a reboot, but I do not want reboots to happen unattended.

This script:

  1. Checks daily (every day at 8.00) if a reboot is required.
  2. Sends an e-mail notification when this is the case.
sudo nano /usr/local/sbin/email_reboot_required

On systems with a mailserver capable of sending outgoing messages

if [ -f /var/run/reboot-required ]; then
    echo "A reboot is required following updates to server `hostname -f` for packages: \n \n $(cat /var/run/reboot-required.pkgs)" | mail -s "Reboot Required" admin@example.org
fi

On systems without a mailserver capable of sending outgoing messages

if [ -f /var/run/reboot-required ]; then
    echo "A reboot is required following updates to server `hostname -f` for packages: \n \n $(cat /var/run/reboot-required.pkgs)" | \
    swaks --to admin@example.org \
    --from "server@example.com" \
    --server smtp.example.org:587 \
    --auth PLAIN \
    --auth-user "server@example.org" \
    --auth-password "____PASSWORD____" \
    --tls \
    --header "Subject: Reboot required" \
    --body \
    --hide-all
fi
sudo apt install swaks
sudo chmod +x /usr/local/sbin/email_reboot_required
sudo crontab -e
00 08 * * * /usr/local/sbin/email_reboot_required