Skip to content

Instantly share code, notes, and snippets.

@netstx
Created May 12, 2021 02:51
Show Gist options
  • Save netstx/d2b71fa8ad7272cac75111945fb32d33 to your computer and use it in GitHub Desktop.
Save netstx/d2b71fa8ad7272cac75111945fb32d33 to your computer and use it in GitHub Desktop.
Cronic is a small shim shell script for wrapping cron jobs so that cron only sends email when an error has occurred.
#!/bin/bash
# Cronic v3 - cron job report wrapper
# Copyright 2007-2016 Chuck Houpt. No rights reserved, whatsoever.
# Public Domain CC0: http://creativecommons.org/publicdomain/zero/1.0/
set -eu
TMP=$(mktemp -d)
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
echo "Cronic 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 -rf "$TMP"
@netstx
Copy link
Author

netstx commented May 12, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment