【问题标题】:cron stop sending error mails if command output is redirected to logfile如果命令输出被重定向到日志文件,cron 停止发送错误邮件
【发布时间】:2020-05-19 05:00:08
【问题描述】:

我对来自 cron 的错误邮件有疑问
如果我创建两个作业来执行相同的 perl 脚本,一个重定向到 /dev/null 或一个日志文件,一个没有重定向,我只会收到一封没有重定向的错误邮件

/etc/cron.d/test-cron

MAILTO="logs@example.com"
*   *   *   *   *   root    /root/test.sh > /dev/null
*   *   *   *   *   root    /root/test.sh
/root/test.sh

#!/usr/bin/perl
use strict;

print "test\n";
exit 1;

cron 和 postfix 的系统日志输出

May 18 19:14:01 cron-master CRON[31428]: (root) CMD ([31436] /root/test.sh)
May 18 19:14:01 cron-master CRON[31428]: (CRON) error (grandchild #31436 failed with exit status 1)
May 18 19:14:01 cron-master CRON[31428]: (root) END ([31436] /root/test.sh)
May 18 19:14:01 cron-master CRON[31429]: (root) CMD ([31439] /root/test.sh > /dev/null)
May 18 19:14:01 cron-master CRON[31429]: (CRON) error (grandchild #31439 failed with exit status 1)
May 18 19:14:01 cron-master CRON[31429]: (root) END ([31439] /root/test.sh > /dev/null)
May 18 19:14:01 cron-master postfix/pickup[28859]: 5537251A9: uid=0 from=<root>
May 18 19:14:01 cron-master postfix/cleanup[30966]: 5537251A9: message-id=<20200518191401.5537251A9@cron-master@example.com>
May 18 19:14:01 cron-master postfix/qmgr[143]: 5537251A9: from=<cron-master@example.com>, size=674, nrcpt=1 (queue active)
May 18 19:14:01 cron-master postfix/smtp[30968]: 5537251A9: to=<logs@example.com>, relay=smtp.example.com[80.50.67.97]:587, delay=0.42, delays=0.02/0/0.32/0.09, dsn=2.0.0, status=sent (250 Requested mail action okay, completed: id=1Ma1oK-1jXP8H2tyW-00W08q)
May 18 19:14:01 cron-master postfix/qmgr[143]: 5537251A9: removed
  • cron -> 3.0pl1-136ubuntu1
  • 后缀-> 3.4.10-1ubuntu1
  • 操作系统 -> 最新的 Ubuntu 20.04 docker 镜像 (ubuntu:focal-20200423)
  • Docker 端点 -> /usr/sbin/cron -f -l -L 15

【问题讨论】:

    标签: linux cron postfix-mta


    【解决方案1】:

    您可以使用tee 命令来“分叉”标准输出。

    *   *   *   *   *   root    /root/test.sh | tee -a logfile_name
    

    man tee

    tee - 从标准输入读取并写入标准输出和文件
    概要
    tee [选项]...[文件]...
    说明
    将标准输入复制到每个 FILE,也复制到标准输出。

    -a,--追加
    附加到给定的文件,不要覆盖

    【讨论】:

    • 非常感谢这也与* * * * * root /root/test.sh 2&gt;&amp;1 | tee -a logfile_name 一起工作以捕获错误
    猜你喜欢
    • 2010-09-18
    • 2020-10-14
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 2013-12-21
    • 2016-10-07
    • 2014-05-18
    相关资源
    最近更新 更多