
Existe a possibilidade de o servidor Ubuntu 20.04 LTS enviar um e-mail para endereços de e-mail específicos quando uma atualização for instalada que exija a reinicialização do servidor?
Então, por exemplo, quando o arquivo /var/run/reboot-required.pkgs existe, ele envia um e-mail aos administradores para que eles possam reinicializar?
Procurei em todo o Google, mas não consigo encontrar um tutorial fácil para isso
Responder1
Tente /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";
Na verdade, essa é a parte fácil. Em seguida, devemos configurar um sistema postal leve para tirar o e-mail do servidor e enviá-lo para você.
Aqui está uma maneira de fazer isso:
sudo apt install msmtp msmtp-mta bsd-mailx
Arquivo de configuração:/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
Comando de teste:
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
Responder2
Obrigado, segui o tutorial e editei um cronjob para isso, então, como usuário root, criei o arquivo de configuração /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
Depois criei um arquivo em /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" [email protected] < /tmp/body.txt; rm /tmp/body.txt
fi
E quando a máquina tem uma atualização que requer reinicialização e cria o arquivo /var/run/reboot-required, ela me envia um e-mail para me notificar sobre a reinicialização