Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mskf3000/d9a2fdd97896736ef8fe0452c66c4f55 to your computer and use it in GitHub Desktop.
Save mskf3000/d9a2fdd97896736ef8fe0452c66c4f55 to your computer and use it in GitHub Desktop.
Bash stdout to file - Logger

Add the code below to .sh file.

# Optional:
# exec 1<&- # Close STDOUT file descriptor
# exec 2<&- # Close STDERR FD

parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P ) 
LOG_FILE=$parent_path'/error.log' #log file name
exec 1>> $LOG_FILE  # Open STDOUT as $LOG_FILE file for read and write.
exec 2>&1   # Redirect STDERR to STDOUT
echo | date "+%Y-%m-%d %H:%M:%S"

# example:
echo "Time Stamp should be above this line"
cat nofile.example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment