Cron job stderr to email AND log file?

前端 未结 9 1688
离开以前
离开以前 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条回答
  •  庸人自扰
    2021-01-06 08:56

    Unless I'm missing something:

    command 2>&1 >> file.log | tee -a file.log
    

    2>&1 redirects stderr to stdout

    >> redirects regular command stdout to logfile

    | tee duplicates stderr (from 2>&1) to logfile and passes it through to stdout be mailed by cron to MAILTO

    I tested it with

    (echo Hello & echo 1>&2 World) 2>&1 >> x | tee -a x
    

    Which indeed shows World in the console and both texts within x

    The ugly thing is the duplicate file name. And the different buffering from stdout/stderr might make text in file.log a bit messy I guess.

提交回复
热议问题