Skip to content

Instantly share code, notes, and snippets.

@fire1ce
Last active November 4, 2023 06:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fire1ce/068cbf0802955fb2422fb7a64772b11a to your computer and use it in GitHub Desktop.
Save fire1ce/068cbf0802955fb2422fb7a64772b11a 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