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

tru*_*ype 3 server bash cron postfix

这是我的内容 sudo crontab -e

    @hourly /home/userName/ntpdate.sh
Run Code Online (Sandbox Code Playgroud)

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

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

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

我在 /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
Run Code Online (Sandbox Code Playgroud)

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

我知道 ntpdate 已被弃用,但它是我现在唯一需要的东西(假设我可以让 cron 运行它)。

Let*_*ety 8

根据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)
Run Code Online (Sandbox Code Playgroud)

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

因此,您应该将 cron 文件修改为:

  @hourly /home/userName/ntpdate.sh >> /home/userName/ntpdateLog.txt 2>&1
Run Code Online (Sandbox Code Playgroud)

和你的脚本:

  echo "Current time is $(date), "
  ntpdate -u 192.168.1.25
Run Code Online (Sandbox Code Playgroud)

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

  MAILTO=""
  @hourly /home/userName/ntpdate.sh >> /home/userName/ntpdateLog.txt 2>&1
Run Code Online (Sandbox Code Playgroud)

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

这是crontab 手册以获取更多详细信息。