Lassen Sie den Ubuntu-Server eine E-Mail senden, wenn ein Systemneustart erforderlich ist

Lassen Sie den Ubuntu-Server eine E-Mail senden, wenn ein Systemneustart erforderlich ist

Gibt es eine Möglichkeit, dass Ubuntu Server 20.04 LTS eine E-Mail an bestimmte E-Mail-Adressen senden kann, wenn ein Update installiert wurde, das einen Serverneustart erfordert?

Wenn also beispielsweise die Datei /var/run/reboot-required.pkgs existiert, wird eine E-Mail an die Administratoren gesendet, damit diese einen Neustart durchführen können?

Ich habe bei Google gesucht, kann aber kein einfaches Tutorial dafür finden

Antwort1

Versuchen Sie /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. "[email protected]"
Unattended-Upgrade::Mail "[email protected]";

// 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";

Das ist eigentlich der einfache Teil. Als Nächstes müssen wir ein einfaches Postsystem einrichten, um die E-Mail vom Server zu Ihnen zu bringen.

So können Sie das tun:

sudo apt install msmtp msmtp-mta bsd-mailx

Konfigurationsdatei:/root/.msmtprc

account        your_label_here
host           smtp.example.com
port           465
from           root@your_machine.your_domain
user           [email protected] (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

Testbefehl:

echo "This is the email body" > /tmp/body.txt && sudo mailx -s "This is the subject" [email protected] < /tmp/body.txt; rm /tmp/body.txt

Antwort2

Danke, ich habe das Tutorial befolgt und einen Cronjob dafür bearbeitet, sodass ich als Benutzer root die Konfigurationsdatei /root/.msmtprc erstellt habe.

account        your_label_here
host           smtp.example.com
port           465
from           root@your_machine.your_domain
user           [email protected] (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

Anschließend habe ich eine Datei in /etc/cron.hourly/reboot-check erstellt

#!/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" [email protected] < /tmp/body.txt; rm /tmp/body.txt
fi

Und wenn der Computer ein Update hat, das einen Neustart erfordert und die Datei /var/run/reboot-required erstellt, sendet es mir eine E-Mail, um mich über den Neustart zu benachrichtigen

verwandte Informationen