在 CentOS 系统中,你可以使用 mailx 命令来发送邮件通知。首先,确保你已经安装了 mailx。如果没有安装,可以使用以下命令安装:
sudo yum install mailx
接下来,你需要配置邮件传输代理(MTA),例如 Postfix 或 Sendmail。这里以 Postfix 为例:
sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix
sudo vi /etc/mail.rc
在文件末尾添加以下内容,将 your_email@example.com 替换为你的实际邮件地址:
set from=your_email@example.com
set smtp=smtp.example.com
set smtp-auth=login
set smtp-auth-user=your_email@example.com
set smtp-auth-password=your_email_password
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb
保存并退出。
现在你可以使用 mailx 命令发送邮件通知。例如,创建一个名为 send_notification.sh 的脚本:
#!/bin/bash
subject="定时任务通知"
recipient="recipient@example.com"
body="这是一条定时任务通知邮件。"
echo -e "Subject: $subject\n$body" | mailx -s "$subject" "$recipient"
给脚本添加可执行权限:
chmod +x send_notification.sh
现在,你可以在 crontab 中添加定时任务来调用这个脚本。编辑 crontab 配置:
crontab -e
添加一行,例如每天早上 9 点发送通知:
0 9 * * * /path/to/send_notification.sh
保存并退出。现在,定时任务将按照指定的时间发送邮件通知。