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

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

    add this at the beginning of your script

    #!/bin/bash
    set -e 
    outfile=logfile
    
    exec > >(cat >> $outfile)
    exec 2> >(tee -a $outfile >&2)
    
    # write your code here
    

    STDOUT and STDERR will be written to $outfile, only STDERR will be seen on the console

提交回复
热议问题