I'm assuming you want to still see STDERR and STDOUT on the terminal. You could go for Josh Kelley's answer, but I find keeping a tail
around in the background which outputs your log file very hackish and cludgy. Notice how you need to keep an exra FD and do cleanup afterward by killing it and technically should be doing that in a trap '...' EXIT
.
There is a better way to do this, and you've already discovered it: tee
.
Only, instead of just using it for your stdout, have a tee for stdout and one for stderr. How will you accomplish this? Process substitution and file redirection:
command > >(tee stdout.log) 2> >(tee stderr.log >&2)
Let's split it up and explain:
> >(..)
>(...)
(process substitution) creates a FIFO and lets tee
listen on it. Then, it uses >
(file redirection) to redirect the STDOUT of command
to the FIFO that your first tee
is listening on.
Same thing for the second:
2> >(tee stderr.log >&2)
We use process substitution again to make a tee
process that reads from STDIN and dumps it into stderr.log
. tee
outputs its input back on STDOUT, but since its input is our STDERR, we want to redirect tee
's STDOUT to our STDERR again. Then we use file redirection to redirect command
's STDERR to the FIFO's input (tee
's STDIN).
See http://mywiki.wooledge.org/BashGuide/TheBasics/InputAndOutput