Skip to content

Instantly share code, notes, and snippets.

@walquis
Created May 31, 2016 14:59
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 walquis/cda4677cd96a2098392067446c84a6f7 to your computer and use it in GitHub Desktop.
Save walquis/cda4677cd96a2098392067446c84a6f7 to your computer and use it in GitHub Desktop.
A variation of Chuck Houpt's 2007 cronic script
#!/bin/bash
# Cronic v2 - cron job report wrapper
# Copyright 2007 Chuck Houpt
CRONIC_LOG=${CRONIC_LOG:-/sitelogs/cronic/cronic.$LOGNAME.log}
CRONIC_LOCK=${CRONIC_LOCK:-/tmp/cronic.$LOGNAME.lock}
trim() { echo $1; }
HOST=$(trim `hostname`)
USER=$(trim `whoami`)
# N.B. On ubuntu, `sudo apt-get install heirloom-mailx` for the version of /usr/bin/mail
# for which this script is written
MAIL=mail
MAILTO="a@b.net"
SMTP="smtp.gmail.com"
FROM="$USER@$HOST"
# http://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
set +e # Do not exit immediately if certain commands return non-zero status.
set -u # Treat unset vars and params besides special params .@. or .*. as an error when expanding params.
OUT=/tmp/cronic.$USER.out.$$
ERR=/tmp/cronic.$USER.err.$$
TRACE=/tmp/cronic.$USER.trace.$$
MSG=/tmp/cronic.$USER.msg.$$
"$@" >$OUT 2>$TRACE
EXITCODE=$?
PATTERN="^${PS4:0:1}\\+${PS4:1}"
if grep -aq "$PATTERN" $TRACE ; then
! grep -av "$PATTERN" $TRACE > $ERR
else
ERR=$TRACE
fi
if [ $EXITCODE -ne 0 -o -s "$ERR" ] ; then
(
echo "On host $HOST, cronic detected failure or error output for the command:"
echo "$@"
echo
echo "EXIT CODE: $EXITCODE"
echo
echo "ERROR OUTPUT:"
cat "$ERR"
echo
echo "STANDARD OUTPUT:"
cat "$OUT"
if [ $TRACE != $ERR ] ; then
echo
echo "TRACE-ERROR OUTPUT:"
cat "$TRACE"
fi
) > $MSG
$MAIL -V < /dev/null >/dev/null 2>&1
if [ $? -eq 0 ] ; then # Sending with Ubuntu mail...
cat $MSG | tr '\r' '\n' | $MAIL -s "For '$1', cronic detected either non-zero exit or STDERR output" -S from="$FROM" -S smtp="$SMTP" $MAILTO
else # Sending with RHEL mail...
# Must be a non-heirloom-mailx, so doesn't understand -S option. Hopefully, "from" and "smtp" are configured OK...
cat $MSG | tr '\r' '\n' | $MAIL -s "For '$1', cronic detected either non-zero exit or STDERR output" $MAILTO
fi
fi
(
flock -w 5 200
mkdir -p /sitelogs/cronic 2>/dev/null
if [ $? -ne 0 ]; then CRONIC_LOG=cronic.log; fi
echo "At $(date) executed:" >> $CRONIC_LOG
echo "$@" >> $CRONIC_LOG
echo "Returned exit code $EXITCODE" >> $CRONIC_LOG
echo >> $CRONIC_LOG
echo "STDOUT:" >> $CRONIC_LOG
cat "$OUT" >> $CRONIC_LOG
echo >> $CRONIC_LOG
echo "STDERR:" >> $CRONIC_LOG
cat "$ERR" >> $CRONIC_LOG
echo >> $CRONIC_LOG
printf -v f "%20s" ; printf "%s\n" "${f// /-}" >> $CRONIC_LOG
echo >> $CRONIC_LOG
) 200>$CRONIC_LOCK
rm -f "$OUT"
rm -f "$ERR"
rm -f "$TRACE"
rm -f "$MSG"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment