cron 使用什么邮件程序发送邮件?

cbm*_*ica 12 sendmail centos cron centos6

我正在尝试调试 cron 没有在我没有配置的 Centos 6 机器上发送邮件的问题。如何确定 cron 使用哪个邮件程序发送邮件?crontab 手册页有这么一段话:

除了 LOGNAME、HOME 和 SHELL,cron(8) 将查看 MAILTO,如果它有任何理由由于在“这个”crontab 中运行命令而发送邮件。如果 MAILTO 已定义(且非空),则邮件将发送给如此命名的用户。如果 MAILTO 已定义但为空 (MAILTO=""),则不会发送邮件。否则邮件将发送给 crontab 的所有者。 如果您在安装 cron 时决定使用 /bin/mail 而不是 /usr/lib/sendmail 作为邮件程序,则此选项很有用 -- /bin/mail 不执行别名,并且 UUCP 通常不读取其邮件。

带星号的部分是让我想知道的部分:“好吧,是 sendmail 还是 mail?”

小智 25

根据 cron(8)(实际发送消息的守护进程)的手册页:

   -m     This  option  allows you to specify a shell command string to use for 
          sending cron mail output instead of sendmail(8).  This command must 
          accept a fully formatted mail message (with headers) on stdin and send
          it as a mail message to the recipients specified in the mail headers.
Run Code Online (Sandbox Code Playgroud)

这让我相信它默认使用 sendmail。让我们用 strace 验证一下:

设置将生成电子邮件的 cron 作业:

user@host1 ~:
$ crontab -e
crontab: installing new crontab
user@host1 ~:
$ crontab -l
MAILTO=example@example.com
*/5 * * * * echo "testing"
Run Code Online (Sandbox Code Playgroud)

现在找到 crond 的进程 ID:

user@host1 ~:
$ ps auxww | grep crond
root      9684  0.0  0.0 117280  1296 ?        Ss   Jul22   0:17 crond
user     36344  0.0  0.0 103240   884 pts/2    S+   23:01   0:00 grep crond
Run Code Online (Sandbox Code Playgroud)

使用 strace 附加到 crond 进程,查找与进程相关的活动。当 strace 写入 stderr 时,我已将其重定向到 stdout 并搜索“邮件”:

root@host1 ~:
# strace -fp 9684 -s 1024 -e trace=process 2>&1 | grep mail
[pid 36204] execve("/usr/sbin/sendmail", ["/usr/sbin/sendmail", "-FCronDaemon", "-i", "-odi", "-oem", "-oi", "-t", "-f", "root"], [/* 16 vars */]) = 0
^C
Run Code Online (Sandbox Code Playgroud)

是的,它是sendmail。

  • 是的,但在它的默认配置中,配置文件中没有任何引用邮件的内容(减去注释,整个内容是`CRONDARGS=`)。事实上,它是可配置的,这就是为什么我包含了验证自己的步骤的原因。 (6认同)
  • 在您测试的系统上。 (4认同)
  • 对,这是在这个问题上标记的 CentOS,在它的默认配置中。 (4认同)
  • 我知道我在这里敲着累了,但它是一个可配置的参数,问题涉及提问者没有设置的系统。对于有问题的系统,邮件程序以前可能已从默认设置更改。询问者知道默认值。 (2认同)

mfi*_*nni 4

快速谷歌显示给我的/etc/sysconfig/crond是定义 cron 使用什么邮件程序的文件。