Skip to content

Instantly share code, notes, and snippets.

@garymacindoe
Created November 25, 2018 19:24
Show Gist options
  • Save garymacindoe/b17b15a441eb3eab48e6921714914eb2 to your computer and use it in GitHub Desktop.
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
#!/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