简体   繁体   中英

Write stderr and stdout to one file, but also write stderr to a separate file

I have a shell script whose stdout and stderr I want to write to a logfile. I know that this can be achieved via

sh script.sh >> both.log 2>&1

However, I also want to simultaneously write the stderr to a separate file, "error.log". Is this achievable?

You can use tee to duplicate output to two locations. Combine that with some tricky redirections, and...

script.sh 2>&1 >> both.log | tee -a both.log >> error.log

This redirects stderr to stdout, and then stdout to both.log . stderr remains, and is piped to tee , which copies it to both log files.

For this you need to first switch stdout and stderr, which requires an additional file descriptor:

sh script.sh 3>&2 2>&1 1>&3 3>&-

The last operator closes the auxiliary file descriptor.

After that you can use tee to duplicate the error stream (which is now on stdin) and append it to your error log:

sh script.sh 3>&2 2>&1 1>&3 3>&- | tee -a error.log

And after that you can then direct both stdin and stderr to your combined log:

(sh script.sh 3>&2 2>&1 1>&3 3>&- | tee -a error.log) >> both.log 2>&1

The parentheses around the command are important to capture the error stream of the whole command. Without them only the (empty) error stream of the tee command would be captured and the rest would still go to the terminal.

Note: this does not check wheater the file descriptor 3 was in use (open) before. In bash you can use this to choose a previously unused file descriptor and close it on the last redirection:

sh script.sh {tmpfd}>&2 2>&1 1>&${tmpfd}-

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