tee is a GNU tool to piggy back stout off of a pipe. The following example shows the basic pattern for using it, where the stout is printed to the terminal and written to a log file.
mycommand | tee mycommand.log
“teethis” script #
I frequently want to run shell scripts where I can see both the stdout and stderr immediately while preserving the output to refer to later. While the tee command to do this is relatively simple, it can be a pain to type over and over again, so I created the following teethis script.
#!/bin/bash
bash $1 2>&1 | tee $1.log
This simple script takes a bash script as the argument, and runs it while using tee to automatically save the stdout/stderr to a log file using the name of the script so that the log is easy to find.
# chmod +x teethis
teethis mybashscript.sh
# ... mybashscript.sh outputs ...
ls *log
# mybashscript.sh.log
Modifications that might also be frequently useful #
Append to output file, rather than overwriting
#!/bin/bash
bash $1 2>&1 | tee -a $1.log
Run scripts with other interpreters, called like teethis python myscript.py
#!/bin/bash
$1 $2 2>&1 | tee $2.log
Execute shell commands, and include date/time
#!/bin/bash
date >> teethis.log
"$1" 2>&1 | tee -a teethis.log