Write STDOUT & STDERR to a logfile, also write STDERR to screen

后端 未结 5 685
野趣味
野趣味 2020-12-23 23:22

I would like to run several commands, and capture all output to a logfile. I also want to print any errors to the screen (or optionally mail the output to someone).

5条回答
  •  滥情空心
    2020-12-23 23:43

    (./doit >> log) 2>&1 | tee -a log
    

    This will take stdout and append it to log file.

    The stderr will then get converted to stdout which is piped to tee which appends it to the log (if you are have Bash 4, you can replace 2>&1 | with |&) and sends it to stdout which will either appear on the tty or can be piped to another command.

    I used append mode for both so that regardless of which order the shell redirection and tee open the file, you won't blow away the original. That said, it may be possible that stderr/stdout is interleaved in an unexpected way.

提交回复
热议问题