我创建了一个脚本 /etc/cron.hourly/test-hourly 来向我发送电子邮件:
#!/bin/bash
EMAIL_TO="[email protected]"
EMAIL_CC="[email protected]"
SUBJECT="Cron hourly test $(date)"
BODY="Cron hourly test $(date)"
echo "Cron hourly test $(date)"
{
echo "To: $EMAIL_TO"
echo "Cc: $EMAIL_CC"
echo "Subject: $SUBJECT"
echo ""
echo "$BODY"
} | /usr/sbin/sendmail -t
我可以验证 cron 作业是否正在成功执行,因为我可以在 /var/log/cron 下看到以下日志消息
May 23 12:01:01 hostname CROND[26928]: (root) CMDOUT (/etc/cron.hourly/test-hourly:)
May 23 12:01:01 hostname CROND[26928]: (root) CMDOUT ()
May 23 12:01:01 hostname CROND[26928]: (root) CMDOUT (Cron hourly test Tue May 23 12:01:01 MST 2023)
这项工作似乎也没有发送邮件的问题,但出于某种原因,我的收件箱从未收到邮件。以下是 /var/log/maillog 中的条目
May 23 12:01:01 hostname sendmail[26947]: 34NJ11jc026947: from=root, size=156, class=0, nrcpts=1, msgid=<[email protected]>, relay=root@localhost
May 23 12:01:01 hostname sendmail[26948]: 34NJ11ks026948: from=<[email protected]>, size=431, class=0, nrcpts=1, msgid=<[email protected]>, proto=ESMTP, daemon=MTA, relay=localhost [127.0.0.1]
May 23 12:01:01 hostname sendmail[26947]: 34NJ11jc026947: [email protected], ctladdr=root (0/0), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30156, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (34NJ11ks026948 Message accepted for delivery)
May 23 12:01:02 hostname sendmail[26951]: 34NJ11ks026948: to=<[email protected]>, ctladdr=<[email protected]> (0/0), delay=00:00:01, xdelay=00:00:01, mailer=relay, pri=120431, relay=example.example.com. [x.x.x.x], dsn=2.0.0, stat=Sent ( <[email protected]> Queued mail for delivery)
我对 sendmail 进行了单独测试,可以在收件箱中收到电子邮件:
echo "Subject: sendmail test" | sendmail -v [email protected]
我是否遗漏了有关为什么 cron 小时作业的邮件未到达我的收件箱的任何信息?为了以防万一,我还检查了垃圾邮件和垃圾文件夹,但一无所获。
更新 我添加了从命令行手动运行 sendmail 时的日志文件。
在我手动运行 sendmail 并成功在收件箱中收到邮件后,maillog 日志如下所示
May 23 12:42:56 hostname sendmail[31309]: 34NJgu7J031309: from=admin, size=23, class=0, nrcpts=1, msgid=<202305231942.34
[email protected]>, relay=admin@localhost
May 23 12:42:56 hostname sendmail[31310]: 34NJguoT031310: from=<[email protected]>, size=336, class=0, nrcpts=1, msgid=<[email protected]>, proto=ESMTP, daemon=MTA, relay=localhost [127.0.0.1]
May 23 12:42:56 hostname sendmail[31309]: 34NJgu7J031309: [email protected], ctladdr=admin (1001/1001), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30023, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (34NJguoT031310 Message accepted for delivery)
May 23 12:42:57 MWQ1ZWEB4 sendmail[31312]: 34NJguoT031310: to=<[email protected]>, ctladdr=<[email protected]> (1001/1001), delay=00:00:01, xdelay=00:00:01, mailer=relay, pri=120336, relay=hostname.hostname.com. [x.x.x.x], dsn=2.0.0, stat=Sent ( <[email protected]> Queued mail for delivery)
两者的区别在于从cron 作业实例中的字段为 root,手动运行时的字段为 admin(我使用的用户帐户)。我需要配置什么才能使其正常工作?