You can use the debugging function of the shell together with tee
:
( set -x; command1 args...; command2 args ) 2>&1 | tee output.log
( ... )
starts a sub-shell which allows you to “collect” the output streams of all commands executed within the sub-shell. It also contains the effect of theset
command below to this sub-shell.set -x
enables thex
shell option which prints all commands that the shell runs to the standard error stream before running them.2>&1
redirects stream 2 (standard error) to stream 1 (standard output).|
redirects the the standard output stream of the left command to the standard input stream of the right command.tee FILE
copies the standard input stream to the fileFILE
and to standard output.
If your command sequence is already in a script file it would make more sense to run it like this:
bash -x /path/to/script args... 2>&1 | tee output.log