Skip to content

Instantly share code, notes, and snippets.

@fangpsh
Last active August 29, 2015 14:24

Revisions

  1. fangpsh revised this gist Jul 13, 2015. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions cronic.sh
    Original file line number Diff line number Diff line change
    @@ -3,15 +3,16 @@
    # 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

    OUT=/tmp/cronic.out.$$
    ERR=/tmp/cronic.err.$$
    TRACE=/tmp/cronic.trace.$$
    HOSTNAME=`hostname`
    USER=`whoami`

    set +e
    "$@" >$OUT 2>$TRACE
    eval "("$@")" >$OUT 2>$TRACE
    RESULT=$?
    set -e

    @@ -25,7 +26,7 @@ fi

    if [ $RESULT -ne 0 -o -s "$ERR" ]
    then
    echo "Cronic detected failure or error output for the command:"
    echo " $USER@$HOSTNAME: Cronic detected failure or error output for the command:"
    echo "$@"
    echo
    echo "RESULT CODE: $RESULT"
  2. fangpsh created this gist Jul 13, 2015.
    48 changes: 48 additions & 0 deletions cronic.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    #!/bin/bash

    # 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

    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 -f "$OUT"
    rm -f "$ERR"
    rm -f "$TRACE"