当需要重启系统时让 Ubuntu 服务器发送邮件

当需要重启系统时让 Ubuntu 服务器发送邮件

当安装需要重新启动服务器的更新时,Ubuntu 服务器 20.04 LTS 是否有可能向特定电子邮件地址发送邮件

因此,例如当文件 /var/run/reboot-required.pkgs 存在时,它会向管理员发送一封邮件,以便他们可以重新启动?

我搜索了谷歌,但找不到一个简单的教程

答案1

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

这其实是比较简单的部分。接下来,我们必须建立一个轻量级的邮政系统,以便将电子邮件从服务器发送到您手中。

以下是一种方法:

sudo apt install msmtp msmtp-mta bsd-mailx

配置文件:/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

测试命令:

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

答案2

谢谢,我按照教程操作,并为此编辑了一个 cronjob,因此实际上作为 root 用户,我创建了配置文件 /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

之后我在 /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

当机器有更新需要重新启动并创建 /var/run/reboot-required 文件时,它会向我发送一封电子邮件通知我重新启动

相关内容