Skip to content

Instantly share code, notes, and snippets.

@schnittchen
Created January 16, 2016 20:28
Show Gist options
  • Save schnittchen/f31456b70234632400c4 to your computer and use it in GitHub Desktop.
Save schnittchen/f31456b70234632400c4 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Modified to send email instead. Original copyright message below:
# Cronic v2 - cron job report wrapper
# Copyright 2007 Chuck Houpt. No rights reserved, whatsoever.
# Public Domain CC0: http://creativecommons.org/publicdomain/zero/1.0/
set -eu
TO=$1
shift
OUT=/tmp/cronic.out.$$
ERR=/tmp/cronic.err.$$
TRACE=/tmp/cronic.trace.$$
set +e
"$@" >$OUT 2>$TRACE
RESULT=$?
set -e
PATTERN="^${PS4:0:1}\\+${PS4:1}"
if grep -aq "$PATTERN" $TRACE
then
! grep -av "$PATTERN" $TRACE > $ERR
else
ERR=$TRACE
fi
if [ $RESULT -ne 0 -o -s "$ERR" ]
then
CMD=$@
exec > >(mail -s "Cronic-mail output on `hostname -f` for $CMD" $TO)
echo "Cronic-mail detected failure or error output for the command:"
echo "$@"
echo
echo "RESULT CODE: $RESULT"
echo
echo "ERROR OUTPUT:"
cat "$ERR"
echo
echo "STANDARD OUTPUT:"
cat "$OUT"
if [ $TRACE != $ERR ]
then
echo
echo "TRACE-ERROR OUTPUT:"
cat "$TRACE"
fi
fi
rm -f "$OUT"
rm -f "$ERR"
rm -f "$TRACE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment