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

后端 未结 5 687
野趣味
野趣味 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:49

    If your system has /dev/fd/* nodes you can do it as:

    ( exec 5>logfile.txt ; { command1 && command2 && command3 ;} 2>&1 >&5 | tee /dev/fd/5 )
    

    This opens file descriptor 5 to your logfile. Executes the commands with standard error directed to standard out, standard out directed to fd 5 and pipes stdout (which now contains only stderr) to tee which duplicates the output to fd 5 which is the log file.

提交回复
热议问题