Cron 作业未运行-postfix/sendmail 错误

Cron 作业未运行-postfix/sendmail 错误

这是我的 sudo crontab -e 的内容

    @hourly /home/userName/ntpdate.sh

我的脚本(ntpdate.sh)的内容//.25是我的ntp服务器

     echo "Current time is $(date), " >> /home/userName/ntpdateLog.txt
     ntpdate -u 192.168.1.25 >> /home/userName/ntpdateLog.txt

当我单独运行命令或单独运行脚本时,它可以正常工作并输出到文件。该脚本具有正确的运行权限。

我在 /var/log/syslog 中遇到的错误是这样的:

    CRON[6386]: (root) MAIL (mailed 1 byte of output; but got status 0x004b, #012)
    postfix/sendmail[6410]: fatal: open /etc/postfix/main.cf: No such file or directory

为什么当脚本不需要邮件时会出现邮件错误(据我所知)。

我知道 ntpdate 已被弃用,但它是目前唯一能满足我需求的东西(假设我可以让 cron 运行它)。

答案1

根据cron 手册

  When executing commands, any output is  mailed  to  the  owner  of  the
  crontab (or to the user named in the MAILTO environment variable in the
  crontab, if such exists)

如果您想停止邮件警报,您应该重定向标准输出和标准错误。

因此你应该修改你的 cron 文件如下:

  @hourly /home/userName/ntpdate.sh >> /home/userName/ntpdateLog.txt 2>&1

和你的脚本:

  echo "Current time is $(date), "
  ntpdate -u 192.168.1.25

另一种方法是给 cron 文件顶部的 MAILTO 变量一个空值:

  MAILTO=""
  @hourly /home/userName/ntpdate.sh >> /home/userName/ntpdateLog.txt 2>&1

我建议您无论如何重定向 std 输出和 std 错误,因为在出​​现错误的情况下更容易调试脚本。

这是crontab 手册更多细节。

相关内容