0

Is there a possibility that Ubuntu server 20.04 LTS can send a mail to specific email adresses when an update has been installed that require a server reboot

So for instance when the file /var/run/reboot-required.pkgs does exist that it sends a mail to the admins so they can reboot?

I have looked al over Google but cannot find an easy tutorial for this

2 Answers2

1

Try /etc/apt/apt.conf.d/50unattended-upgrades

// Send email to this address for problems or packages upgrades
// If empty or unset then no email is sent, make sure that you
// have a working mail setup on your system. A package that provides
// 'mailx' must be installed. E.g. "user@example.com"
Unattended-Upgrade::Mail "user@example.com";

// Set this value to "true" to get emails only on errors. Default // is to always send a mail if Unattended-Upgrade::Mail is set Unattended-Upgrade::MailOnlyOnError "false";

That's actually the easy part. Next, we must set up a lightweight postal system to get the e-mail off the server and over to you.

Here's one way to do that:

sudo apt install msmtp msmtp-mta bsd-mailx

Config file: /root/.msmtprc

account        your_label_here
host           smtp.example.com
port           465
from           root@your_machine.your_domain
user           user@example.com (your email)
password       your_smtp_password
auth           on
tls            on
tls_starttls   off
tls_certcheck  off
logfile        /root/.msmtp.log
account default : your_label_here

Test command:

echo "This is the email body" > /tmp/body.txt && sudo mailx -s "This is the subject" user@example.com < /tmp/body.txt; rm /tmp/body.txt
user535733
  • 68,493
-1

Thanks I have followed the tutorial and edited a cronjob for this so infact as user root I have created the config file /root/.msmtprc

account        your_label_here
host           smtp.example.com
port           465
from           root@your_machine.your_domain
user           user@example.com (your email)
password       your_smtp_password
auth           on
tls            on
tls_starttls   off
tls_certcheck  off
logfile        /root/.msmtp.log
account default : your_label_here

Afterwards I created a file in /etc/cron.hourly/reboot-check

#!/usr/bin/env bash

if [ -f /var/run/reboot-required ]; then echo "A reboot is required following updates to server <TestServer> the machine will auto reboot in 1h" > /tmp/body.txt && sudo mailx -s "Reboot Required" receivers@adress.com < /tmp/body.txt; rm /tmp/body.txt fi

And when the machine has an update that requires a reboot and creates the /var/run/reboot-required file it sends me an email to notify me of the reboot