2025年5月17日星期六

Ubuntu 24.04 配置Crontab任务失败时邮件提醒

 
安装
sudo apt update && sudo apt install -y msmtp msmtp-mta

安装过程中弹出来的Configuring msmtp 选择No


配置(编辑后保存)

nano ~/.msmtprc

添加下面配置内容


account default
host smtp.163.com
port 465
from yourname@163.com
auth on
user yourname@163.com
password YOUR_CLIENT_AUTH_PASSWORD  # 邮箱授权密码
tls on
tls_starttls off
logfile ~/.msmtp.log


赋予读写权限

chmod 600 ~/.msmtprc


测试邮件发送
echo -e "Subject: Test\n\nThis is a test" | msmtp -a default yourname@163.com


检查日志
cat ~/.msmtp.log  # 看到状态码250表示成功


创建别名(替代系统 mail 命令)

nano ~/.bashrc


最末尾添加一行:

alias mail='msmtp -t'


使其环境变量生效
source ~/.bashrc


使用别名测试发送


echo -e "To: xxxxx@163.com\nSubject: Test\n\nThis is a test email" | mail


看日志验证发送结果


cat ~/.msmtp.log


crontab定时任务配置

crontab -e      首行添加一行

MAILTO="xxxxx@163.com"


* * * * * /task1.sh
0 * * * * /task2.sh  

...

task1 或者task2任务 只要执行失败就会发送到MAILTO设置的邮箱地址提醒


结束

没有评论:

发表评论

XG-040G-XX SuPassword 解密工具

 买了新光猫 XG-040G-TF ,  所以写了一个 SuPassword 解密工具, 以备不时之需 . 点击下载