Cron 作业 stderr 到电子邮件和日志文件? [英] Cron job stderr to email AND log file?

查看:27
本文介绍了Cron 作业 stderr 到电子邮件和日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一份 Cron 工作:

I have a cron job:

$SP_s/StartDailyS1.sh >$LP_s/MirrorLogS1.txt

其中 SP_s 是脚本的路径,LP_s 是日志文件的路径.这会将 stdout 发送到日志文件并将 stderr 发送到我的电子邮件.

Where SP_s is the path to the script and LP_s is the path for the log file. This sends stdout to the log file and stderr to my email.

我该怎么做?:
1) 将标准输出和标准错误发送到日志文件,
2)并将标准错误发送到电子邮件

How do I?:
1) send both stdout AND stderr to the logfile,
2) AND send stderr to email

或者换一种说法:日志文件和电子邮件的标准错误,以及日志文件的标准输出.

or to put it another way: stderr to both the logfile and the email, and stdout only to the logfile.

更新:到目前为止,我得到的答案都没有符合我设定的标准,或者似乎不适合 CRON 工作.

UPDATE: None of the answers I've gotten so far either follow the criteria I set out or seem suited to a CRON job.

我看到了这个,它的目的是将命令中的 STDOUT 和 STDERR 发送到一个文件,然后将 STDERR 发送到另一个文件"(由 zazzybob 在 unix.com 上发布),这似乎与我想要的很接近做,我想知道它是否会激发比我更聪明的人:

I saw this, which is intended to "send the STDOUT and STDERR from a command to one file, and then just STDERR to another file" (posted by zazzybob on unix.com), which seems close to what I want to do and I was wondering if it would inspire someone more clever than I:

(( my_command 3>&1 1>&2 2>&3 ) | tee error_only.log ) > all.log 2>&1 

我希望 cron 将 STDERR 发送到电子邮件而不是另一个文件".

I want cron to send STDERR to email rather than 'another file'.

推荐答案

我的经验 (ubuntu) 是 'crontab' 只发送电子邮件 'stderr'(我将输出定向到一个日志文件,然后将其存档).这很有用,但我想要确认脚本已运行(即使stderr"没有错误),以及有关花费多长时间的一些详细信息,我认为这是发现潜在问题的好方法.

My experience (ubuntu) is that 'crontab' only emails 'stderr' (I have the output directed to a log file which is then archived). That is useful, but I wanted a confirmation that the script ran (even when no errors to 'stderr'), and some details about how long it took, which I find is a good way to spot potential trouble.

我发现最容易解决这个问题的方法是编写脚本,其中包含一些重复的回声".广泛的常规回声"最终出现在日志文件中.对于我想要在我的 crontab 'MAILTO' 电子邮件中的重要非错误位,我使用了一个 'echo',它通过 '1>&2' 定向到 stderr.

I found the way I could most easily wrap my head around this problem was to write the script with some duplicate 'echo's in it. The extensive regular 'echo's end up in the log file. For the important non-error bits I want in my crontab 'MAILTO' email, I used an 'echo' that is directed to stderr with '1>&2'.

这样:

    Frmt_s="+>>%y%m%d %H%M%S($HOSTNAME): " # =Format of timestamp: "<YYMMDD HHMMSS>(<machine name>): "
    echo `date "$Frmt_s"`"'$0' started." # '$0' is path & filename
    echo `date "$Frmt_s"`"'$0' started."  1>&2 # message to stderr

# REPORT:
    echo ""
    echo "================================================"
    echo "================================================" 1>&2 # message to stderr
    TotalMins_i=$(( TotalSecs_i / 60 )) # calculate elapsed mins
    RemainderSecs_i=$(( TotalSecs_i-(TotalMins_i*60) ))
    Title_s="TOTAL run time"
    Summary_s=$Summary_s$'
'$(printf "%-20s%3s:%02d" "$Title_s" $TotalMins_i $RemainderSecs_i)
    echo "$Summary_s"
    echo "$Summary_s" 1>&2 # message to stderr
    echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" 1>&2 # message to stderr
    echo ""
    echo `date "$Frmt_s"`"TotalSecs_i: $TotalSecs_i"
    echo `date "$Frmt_s"`"'$0' concluded." # '$0' is path & filename
    echo `date "$Frmt_s"`"'$0' concluded."  1>&2 # message to stderr

向我发送一封包含此内容的电子邮件(当没有错误时,不会出现以ssh:"和rsync:"开头的行):

Sends me an email containing this (when there are no errors, the lines beginning 'ssh:' and 'rsync:' do not appear):

170408 030001(sb03): '/mnt/data1/LoSR/backup_losr_to_elwd.sh' started.
ssh: connect to host 000.000.000.000 port 0000: Connection timed out
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: unexplained error (code 255) at io.c(226) [Receiver=3.1.0]
ssh: connect to host 000.000.000.000 port 0000: Connection timed out
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: unexplained error (code 255) at io.c(226) [Receiver=3.1.0]
================================================
S6 SUMMARY (mins:secs):
'Templates'           2:07
'Clients'             2:08
'Backups'             0:10
'Homes'               0:02
'NetAppsS6'          10:19
'TemplatesNew'        0:01
'S6Www'               0:02
'Aabak'               4:44
'Aaldf'               0:01
'ateam.ldf'           0:01
'Aa50Ini'             0:02
'Aadmin50Ini'         0:01
'GenerateTemplates'   0:01
'BackupScripts'       0:01
TOTAL run time       19:40
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
170408 031941(sb03): '/mnt/data1/LoSR/backup_losr_to_elwd.sh' concluded.

这不能满足我最初的愿望,即将 stdout 和 stderr 都发送到日志文件"(stderr,只有带有 '1>&2' 的回显行发送到电子邮件;stdout 发送到log),但我发现这比我最初想象的解决方案要好,因为电子邮件找到了我,我不必去日志文件中查找问题.

This doesn't satisfy my initial desire to "send both stdout AND stderr to the logfile" (stderr, and only the 'echo'ed lines with '1>&2' go to the email; stdout goes to the log), but I find this is better than my initially imagined solution, as the email finds me and I don't have to go looking for problems in the log file.

这篇关于Cron 作业 stderr 到电子邮件和日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆