【问题标题】:Pipe cronjob errors to mail通过管道将 cronjob 错误发送到邮件
【发布时间】:2013-10-18 21:06:33
【问题描述】:

我有一份 root 的工作

0 0 * * * /usr/bin/time /path/to/mysqlbackup.sh | /bin/mail -s "MySQL Backup" "admin@example.com"

脚本在运行时会回显一些信息,然后按预期通过电子邮件发送。

问题是如果发生错误,它不会被发送到电子邮件,所以我无法捕捉到它。如果我查看 root 用户的 mail,我会看到类似

的错误

mysqldump: Got error: 2002: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 尝试连接时

如何将脚本产生的所有输出发送到我的电子邮件?


注意事项

定时任务

* * * * * (/usr/bin/time /root/utils/test.sh 2&>1) | /usr/bin/tee /tmp/cron.test | /bin/mail -s "test" "srobbins@example.com"

在 root 的邮件中找到消息

[root@example utils]# mail
Mail version 8.1 6/6/93.  Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N  1 root@example  Fri Oct 11 08:35  23/1025  "Cron <root@example> (/usr/bin/time /root/utils/test.sh 2&>1) | /usr/bin/tee /tmp/cron.test | /bin/mail -s "test" "srobbins@example.com""
& 
Message 1:
From root@example.com  Fri Oct 11 08:35:01 2013
Date: Fri, 11 Oct 2013 08:35:01 -0700
From: root@example.com (Cron Daemon)
To: root@example.com
Subject: Cron <root@example> (/usr/bin/time /root/utils/test.sh 2&>1) | /usr/bin/tee /tmp/cron.test | /bin/mail -s "test" "srobbins@example.com"
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>

Null message body; hope that's ok

/root/utils/test.sh的内容

echo "Starting backup at $(date)" 
mysql -u potato -e "show status" # produces error
FILE="/path/to/file.bak"
FILESIZE=`du -h $FILE | cut -f1`
echo $FILESIZE

/tmp/cron.test 为空

已发送邮件的正文为空

【问题讨论】:

    标签: bash email cron stdout centos5


    【解决方案1】:

    基本上你需要像处理 STDOUT 一样处理 STDERR。

    在 cron 中使用管道

    0 0 * * * (/usr/bin/time /path/to/mysqlbackup.sh 2&>1) | /bin/mail -s "MySQL Backup" "admin@example.com" 
    

    这适用于 Ubuntu,您报告它不适用于您(空邮件正文)。我有兴趣深入研究,但与此同时......

    调整你的脚本

    您可以通过重定向 STDERR 使您的脚本仅打印到 STDOUT(至少对于该命令到 STDOUT)

    echo "Starting backup at $(date)" 
    mysql -u potato -e "show status" 2>&1 # produces error we want, so redirect it
    FILE="/path/to/file.bak"
    FILESIZE=`du -h $FILE | cut -f1`
    echo $FILESIZE
    

    如果您希望将脚本中的任何错误发送到 STDOUT,您可以通过在脚本顶部添加以下内容将 STDERR 全局重定向到 STDOUT:

    exec 2>&1
    

    玩具示例

    job.sh

    #prints out to STDOUT and STDERR
    echo good
    echo error >&2
    echo good
    

    script.sh

    #this runs job.sh redirecting STDERR to STDOUT
    sh job.sh 2>&1
    

    现在运行 job.sh,各种重定向显示其打印到 STDOUT 和 STDERR

    $sh job.sh > /dev/null 
    error
    $sh job.sh 2> /dev/null 
    good
    good
    

    运行 script.sh 显示它仅打印到 STDOUT,即使 job.sh 正在打印到 STDERR

    $sh script.sh > /dev/null 
    $sh script.sh 2> /dev/null 
    good
    error
    good
    

    【讨论】:

    • 这是一封正文为空的电子邮件
    • 好的,在 Ubuntu 上为我工作,你使用什么操作系统?要调试发送到邮件命令的内容,您可以试试这个 ' 0 0 * * * (/usr/bin/time /path/to/mysqlbackup.sh 2&>1) | /usr/bin/tee /tmp/cron.test | /bin/mail -s "MySQL Backup" "admin@example.com" ' (在 /tmp/cron.test 中有什么结果)?
    • 感谢您的帮助。 exec 2&gt;&amp;1 最终成为我的解决方案。
    【解决方案2】:

    您也可以使用MAILTO 变量:

    MAILTO="admin@example.com"
    0 0 * * * /usr/bin/time /path/to/mysqlbackup.sh
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多