Skip to content

Instantly share code, notes, and snippets.

@fortran01
Created October 9, 2019 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fortran01/ddefcc46b02500ec28de17e0ac088585 to your computer and use it in GitHub Desktop.
Save fortran01/ddefcc46b02500ec28de17e0ac088585 to your computer and use it in GitHub Desktop.
Log to file and see stdout
#!/bin/bash
logfile="my.log"
# Set up a named pipe for logging
npipe=/tmp/$$.tmp
mknod $npipe p
# Log all output to a log for error checking
tee <$npipe $logfile &
exec 1>&- # close stdout, produces 'write error: Bad file descriptor' if not re-opened below
exec 1>$npipe 2>&1 # 2>&1 redirect stderr to stdout
trap "rm -f $npipe" EXIT
# test
ls -al
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment