简体   繁体   中英

How to save STDERR and STDOUT of a pipeline on a file?

I'm running a pipeline of commands that have STDERR and STDOUT outputs. I want to save both outputs in a single log file.

This are my attempts to do it:

bash my_script.sh > log.txt   #Only save STDOUT
bash my_script.sh > >(tee log.txt) 2> >(tee log.txt >&2)  #The STDERR overwrite the STDOUT  

I hope you can provide a simple solution to do this. Thanks for your time!

bash my_script.sh > log.txt 2>&1

where 2>&1 redirects stderr to stdout

How about just

bash my_script.sh > >(tee log.txt) 2>&1

Also if you want to append output if log.txt already exists, add -a option to tee

bash my_script.sh > >(tee -a log.txt) 2>&1

It's actually equivalent to bash my_script.sh 2>&1 | tee log.txt bash my_script.sh 2>&1 | tee log.txt or bash my_script.sh 2>&1 | tee -a log.txt bash my_script.sh 2>&1 | tee -a log.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM