如何阻止 Cron 发送有关错误的消息

如何阻止 Cron 发送有关错误的消息

我收到来自 cron 的奇怪邮件:

Return-Path: <[email protected]>
Delivered-To: [email protected]
Received: by domain.com (Postfix, from userid 0)
    id 6F944264D0; Mon, 10 Jan 2011 10:35:01 +0000 (UTC)
From: [email protected] (Cron Daemon)
To: [email protected]
Subject: Cron <root@domain> lynx -dump http://www.domain.com/cron/realqueue
Content-Type: text/plain; charset=ANSI_X3.4-1968
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <LOGNAME=root>
Message-Id: <[email protected]>
Date: Mon, 10 Jan 2011 10:35:01 +0000 (UTC)

/bin/sh: lynx: not found

我在 crontab 文件中有以下 cron 设置:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin


*/5 * * * * lynx -dump http://www.domain.com/cron/realqueue

17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

Lynx 也安装在我的 Ubuntu 上。

Ofc 代替 domain.com 是我的域名,刚刚被替换了。

谢谢 ;)

答案1

您应该检查以确保 lynx 二进制文件位于 PATH 变量中指定的位置之一,或者在 cron 行中使用二进制文件的完整路径。为了抑制错误,您需要将 STDERR 和 STDOUT 重定向到 /dev/null,如下所示:

*/5 * * * * lynx -dump http://www.domain.com/cron/realqueue >/dev/null 2>&1

第一个重定向将 STDOUT 发送到 /dev/null,第二个重定向将 STDERR 与 STDOUT 一起发送(即发送到 /dev/null)。

答案2

真正的问题是这样的:

/bin/sh:lynx:未找到

这说明未找到 lynx,因此 crontab 中的该行甚至未运行。使用 Scrivener 或 blueben 的重定向建议抑制错误,您只是隐藏了您想要实际运行的命令未运行的事实。

一般来说,cron 会在出现错误信息时发送这些电子邮件。抑制电子邮件的最佳方法是修复错误,并且只有当您无法修复错误(例如,脚本总是嘈杂)时,您才应该开始将输出重定向到 /dev/null。

答案3

将 MAILTO 添加到 crontab 的顶部并将其设置为空字符串:

 MAILTO=""

答案4

如果您想要 lynx 二进制文件的路径,最简单的命令是 which lynx

那应该会给你类似/usr/bin/lynx

相关内容