Send copy of a script's output to a file
It could be that your script is producing output to stdout
and stderr
, and you are only getting one of those streams output to your log file.
./my_script.sh | tee log.txt
will indeed output everything to the terminal, but will only dump stdout
to the logfile.
./my_script.sh > log.txt 2>&1
will do the opposite, dumping everything to the log file, but displaying nothing on screen.
The trick is to combine the two with tee
:
./myscript.sh 2>&1 | tee log.txt
This redirects stderr
(2
) into stdout
(1
), then pipes stdout
into tee
, which copies it to the terminal and to the log file.
The zsh
multios equivalent would be:
./myscript.sh >&1 > log.txt 2>&1
That is, redirect stdout both to the original stdout and log.txt (internally via a pipe to something that works like tee
), and then redirect stderr to that as well (to the pipe to the internal tee
-like process).