Created
November 25, 2018 19:24
-
-
Save garymacindoe/b17b15a441eb3eab48e6921714914eb2 to your computer and use it in GitHub Desktop.
How to redirect stdout and stderr to log files in a Bash script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
script=$(basename ${0}) | |
exec > >(tee -ia /var/log/${script}_out.log) # Redirect stdout to logfile and continue to print on stdout | |
exec 2> >(tee -ia /root/${script}_err.log >&2) # Redirect stderr to logfile and continue to print of stderr | |
echo stdout # Appears on stdout and in logfile | |
echo stderr >&2 # Appears on stderr and in logfile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment