Skip to content

Instantly share code, notes, and snippets.

@cjlyth
Last active August 29, 2015 14:01
Show Gist options
  • Save cjlyth/d57d564bbf5d7af5cba5 to your computer and use it in GitHub Desktop.
Save cjlyth/d57d564bbf5d7af5cba5 to your computer and use it in GitHub Desktop.
Snippet to redirect stderr and stdout to a file for a script
#!/usr/bin/env bash
find /tmp -maxdepth 1 -type f -uid $(id -u) -name "*err*.log" -delete
#!/usr/bin/env bash
find /tmp -maxdepth 1 -type f -uid $(id -u) -name "*err*.log" | sort
#!/usr/bin/env bash
# see http://www.tldp.org/LDP/abs/html/x17974.html
log_name="${log_name:-$(basename ${0%.*})}"
log_file="$(mktemp -u -t ${log_name:-script}-err-XXXXXXXXXX --suffix=.log)"
exec 2> ${log_file}
echo "Redirecting errors to ${log_file}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment