I'm currently migrating my CRON scripts to systemd .timer units. Unfortunately systemd does not have the feature of sending mails for failed units, containing the output of the failed command.

Enter checkrun.sh, a simple wrapper script that does exactly this job: executing a given command, capturing its potential output and sending a notification mail containing the former on error.

#!/bin/bash

# defaults
MAILER="sendmail"
MAILTO="$USER"
VERBOSE=0

# usage information
function print_usage() {
    echo "usage: $0 [OPTIONS] COMMAND"
    echo "options:"
    echo "    -s CMD     use CMD as mailer (default: sendmail)"
    echo "    -m MAILTO  set recipient for notification mail (default: \$USER)"
    echo "    -v         send command output even on exit code 0"
    echo "    -h         display this usage information"
}

# parse cmdline options
while getopts ":s:m:vh" OPT; do
    case $OPT in
        s)
            MAILER="$OPTARG"
            ;;
        m)
            MAILTO="$OPTARG"
            ;;
        v)
            VERBOSE=1
            ;;
        h)
            print_usage
            exit 0
            ;;
        \?)
            echo "Invalid option: -$OPTARG." >&2
            exit 1
            ;;
        :)
            echo "Option -$OPTARG requires an argument." >&2
            exit 1
            ;;
    esac
done
shift $(( $OPTIND - 1 ))

# check for COMMAND
if [ -z "$1" ]; then
    echo "error: missing argument."
    print_usage
    exit 1
fi

# execute COMMAND, capture output
LOG="$(mktemp)"
$1 &> "$LOG"
ERR=$?

# evaluate return value
if [ $ERR -ne 0 ]; then
    echo -e "Subject: '$1' FAILED ($ERR)\r\n" | cat - "$LOG" | $MAILER "$MAILTO"
    cat "$LOG"
    exit $ERR
fi

# notify if output was given and verbose mode selected
if [ $VERBOSE -eq 1 ] && [ $(wc -l "$LOG" | cut -d ' ' -f 1) -gt 0 ]; then
    echo -e "Subject: '$1'\r\n" | cat - "$LOG" | $MAILER "$MAILTO"
fi

# clean up
rm -f "$LOG"

exit 0

(A bit too simple, therefore not Git-contained, yet.)

Update: GitHub: checkrun.sh

(updated on Mon 03 December 2018)