Cron job stderr to email AND log file?

前端 未结 9 1685
离开以前
离开以前 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:32

    A bit tricky if you want stdout and stderr combined in one file, with stderr yet tee'd into its own stream.

    This ought to do it (error-checking, clean-up and generalized robustness omitted):

    #! /bin/sh
    
    CMD=..../StartDailyS1.sh
    LOGFILE=..../MirrorLogS1.txt
    FIFO=/tmp/fifo
    
    >$LOGFILE
    
    mkfifo $FIFO 2>/dev/null || :
    
    tee < $FIFO -a $LOGFILE >&2 &
    
    $CMD 2>$FIFO >>$LOGFILE
    

    stderr is sent to a named pipe, picked up by tee(1) where it is appended to the logfile (wherein is also appended your command's stdout) and tee'd back to regular stderr.

提交回复
热议问题