Cron作业stderr到电子邮件和日志文件? [英] Cron job stderr to email AND log file?
问题描述
我有一个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)将stdout和stderr同时发送到日志文件,
2)AND send stderr以电子邮件
How do I?:
1) send both stdout AND stderr to the logfile,
2) AND send stderr to email
或换句话说:stderr同时记录日志文件和电子邮件,stdout只记录到日志文件。
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'.
推荐答案
由于我只是看着T恤的信息页(试图找出如何做同样的事情)
Since I was just looking at the info page for tee (trying to figure out how to do the same thing), I can answer the last bit of this for you.
这是大部分的方式:
(( my_command 3>&1 1>&2 2>&3 ) | tee error_only.log ) > all.log 2>&1
,但将error_only.log替换为>(email_command)
but replace "error_only.log" with ">(email_command)"
(( my_command 3>&1 1>&2 2>&3 ) | tee >(/bin/mail -s "SUBJECT" "EMAIL") ) > all.log 2>&1
注意:根据tee的文档,这将工作在bash,不在/ bin / sh中。如果你把这放在一个cron脚本(如/etc/cron.daily/),那么你可以只是#!/ bin / bash在顶部。然而,如果你把它作为一个线索在crontab然后你可能需要将它包装在bash -c
Note: according to tee's docs this will work in bash, but not in /bin/sh. If you're putting this in a cron script (like in /etc/cron.daily/) then you can just but #!/bin/bash at the top. However if you're putting it as a one-liner in a crontab then you may need to wrap it in bash -c ""
这篇关于Cron作业stderr到电子邮件和日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!