Cron job stderr to email AND log file?

前端 未结 9 1624
离开以前
离开以前 2021-01-06 07:57

I have a cron job:

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

Where SP_s is the path to the script and LP_s

9条回答
  •  -上瘾入骨i
    2021-01-06 08:56

    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.

    This is most of the way there:

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

    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
    

    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 ""

提交回复
热议问题