Skip to content

Instantly share code, notes, and snippets.

@manos
Created October 10, 2013 17:42
Show Gist options
  • Save manos/6922476 to your computer and use it in GitHub Desktop.
Save manos/6922476 to your computer and use it in GitHub Desktop.
bashwrap.sh, to print full 'bash -x' output of the run, iff there was stderr. When run from cron, now you get *context* if something errors out.
#!/bin/bash
#
# runs the provided script/arguments with bash -x, and prints ALL stderr/stdout
# (bash -x output) if any stderr is present. Otherwise, it silently exits.
#
output=/tmp/output-$$
# send stdout + stderr to the logfile, in order, and let stderr flow through.
# run in a sub-process, so the re-mapped stdout/stderr file descriptors are
# closed after our process runs, so we can then grep stderr for anything that
# doesn't start with + (which is what bash -x prepends lines with)..
errs=$(
(exec 1>>$output 2> >(tee -a $output >&2)
bash -x "$@"
) 2> >(grep -v '\+') |wc -l
)
[ $errs -gt 0 ] && cat $output
rm -f $output
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment