我已经为 root 用户设置了 cronjob。但是,当 cron 失败时,我应该收到来自 cronjob 的电子邮件,因为我已在 /etc/crontab 中像这样设置了 MAILTO:
[email protected]
我已经使用安装了 mailx
yum install mailx
但是当我查看 cron 日志时,我得到了以下几行:
Feb 23 14:13:01 internal crond[6858]: (root) CMD (/var/www/vded/build/bin/sync.sh)
Feb 23 14:13:01 internal crond[6857]: (root) MAIL (mailed 79 bytes of output but got status 0x0001 )
我怎样才能解决这个问题?
更新 :
- 我使用 yum 安装了 mailx 和 sendmail。
- 我启动了 sendmail。
- 因此现在可以通过命令行发送电子邮件了!
我在邮件日志中收到此错误:
Feb 23 16:06:01 internal sendmail[7210]: o1NG61cd007210: from=root, size=425, class=0, nrcpts=1, msgid=<[email protected]>, relay=root@localhost
Feb 23 16:06:01 internal sendmail[7211]: o1NG61hT007211: from=<[email protected]>, size=732, class=0, nrcpts=1, msgid=<[email protected]>, proto=ESMTP, daemon=MTA, relay=localhost [127.0.0.1]
Feb 23 16:06:01 internal sendmail[7210]: o1NG61cd007210: to=root, ctladdr=root (0/0), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30425, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (o1NG61hT007211 Message accepted for delivery)
Feb 23 16:06:01 internal sendmail[7212]: o1NG61hT007211: to=<[email protected]>, ctladdr=<[email protected]> (0/0), delay=00:00:00, xdelay=00:00:00, mailer=local, pri=30972, dsn=2.0.0, stat=Sent
internal.fff.com 是服务器的名称。
更新 :
因此事实上如果我将其[email protected]
作为我的 crontab 的第一行(使用crontab -e
),它就可以工作。
MAILTO
为什么设置了之后不起作用/etc/crontab
?
谢谢
答案1
因此回答我的问题:
- 安装 mailx
yum install mailx
- 安装 sendmail
yum install sendmail
- 启动 sendmail
- 使用:将该
MAILTO
行放入 crontab 中,而不是crontab -e
[email protected]
/etc/crontab
答案2
您可以通过安装 nullmailer 而不是 sendmail(或 exim4、postfix 等)来简化此操作。
如果它不起作用(就像我的一样),并且您正在向 gmail 等网络邮件服务发送邮件:请确保您没有在其上设置任何过滤器。事实上,我对 cron 中的任何内容都设置了过滤器,以绕过收件箱。这就是为什么我没有看到 cron 作业中的任何内容。
答案3
我的/var/log/maillog
日志显示crontab
(带有MAILTO=root
)试图向root
和 发送电子邮件root@my_fqdn
(其中my_fqdn
类似于 server.something.com)。根据日志,发送到root
成功和root@my_fqdn
失败,但我在 中没有找到消息/var/mail/root
。以下是相关/var/log/maillog
代码片段:
Aug 16 17:49:02 ip-10-64-21-125 sendmail[22211]: v7GHn2Tr022211: from=root, size=742, class=0, nrcpts=1, msgid=<201708161749.v7GHn2Tr022211@my_fqdn>, relay=root@localhost
Aug 16 17:49:02 ip-10-64-21-125 sendmail[22212]: v7GHn2Ws022212: from=<root@my_fqdn>, size=1061, class=0, nrcpts=1, msgid=<201708161749.v7GHn2Tr022211@my_fqdn>, proto=ESMTP, daemon=MTA, relay=localhost [127.0.0.1]
Aug 16 17:49:02 ip-10-64-21-125 sendmail[22211]: v7GHn2Tr022211: to=root, ctladdr=root (0/0), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30742, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (v7GHn2Ws022212 Message accepted for delivery)
Aug 16 17:49:02 ip-10-64-21-125 sendmail[22214]: v7GHn2Ws022212: to=<root@my_fqdn>, delay=00:00:00, xdelay=00:00:00, mailer=esmtp, pri=121061, relay=my_fqdn. [10.0.0.1], dsn=4.0.0, stat=Deferred: Connection refused by my_fqdn.
然而,根据此提示,邮件现在显示在/var/mail/root
。该问题/etc/hosts
需要添加一行,以便my_fqdn
解决作为一个域(注意末尾的点):
127.0.0.1 my_fqdn.
笔记:现在的工作方式是,消息仍然无法正确接收,但会以某种方式反弹/var/mail/root
(请注意stat=Local configuration error
下面的/var/log/maillog
代码片段)。
Aug 16 17:50:04 ip-10-64-21-125 sendmail[22223]: v7GHo4ZJ022223: from=root, size=742, class=0, nrcpts=1, msgid=<201708161750.v7GHo4ZJ022223@my_fqdn>, relay=root@localhost
Aug 16 17:50:04 ip-10-64-21-125 sendmail[22224]: v7GHo4MT022224: from=<root@my_fqdn>, size=1061, class=0, nrcpts=1, msgid=<201708161750.v7GHo4ZJ022223@my_fqdn>, proto=ESMTP, daemon=MTA, relay=localhost [127.0.0.1]
Aug 16 17:50:04 ip-10-64-21-125 sendmail[22223]: v7GHo4ZJ022223: to=root, ctladdr=root (0/0), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30742, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (v7GHo4MT022224 Message accepted for delivery)
Aug 16 17:50:04 ip-10-64-21-125 sendmail[22226]: v7GHo4MT022224: SYSERR(root): my_fqdn. config error: mail loops back to me (MX problem?)
Aug 16 17:50:04 ip-10-64-21-125 sendmail[22226]: v7GHo4MT022224: to=<root@my_fqdn>, delay=00:00:00, xdelay=00:00:00, mailer=esmtp, pri=121061, relay=my_fqdn. [127.0.0.1], dsn=5.3.5, stat=Local configuration error
Aug 16 17:50:04 ip-10-64-21-125 sendmail[22226]: v7GHo4MT022224: v7GHo4MT022226: DSN: Local configuration error
Aug 16 17:50:04 ip-10-64-21-125 sendmail[22227]: v7GHo4Ml022227: localhost [127.0.0.1] did not issue MAIL/EXPN/VRFY/ETRN during connection to MTA
Aug 16 17:50:04 ip-10-64-21-125 sendmail[22226]: v7GHo4MT022226: to=root, delay=00:00:00, xdelay=00:00:00, mailer=local, pri=62294, dsn=2.0.0, stat=Sent
Aug 16 17:50:04 ip-10-64-21-125 sendmail[22226]: v7GHo4MT022226: to=<root@my_fqdn>, delay=00:00:00, xdelay=00:00:00, mailer=esmtp, pri=62294, relay=my_fqdn., dsn=5.3.5, stat=Local configuration error
Aug 16 17:50:04 ip-10-64-21-125 sendmail[22226]: v7GHo4MT022226: v7GHo4MU022226: return to sender: Local configuration error
Aug 16 17:50:04 ip-10-64-21-125 sendmail[22226]: v7GHo4MU022226: to=root, delay=00:00:00, xdelay=00:00:00, mailer=local, pri=33318, dsn=2.0.0, stat=Sent