#!/bin/bash # cron-harness # by Jon Jensen # $Id: cron-harness,v 1.3 2005/12/31 15:28:25 jon Exp $ # # Invocation: # cron-harness [ -e email@address ] some-program and its args # # Run some program. If it returns an error exit value, either email its # output somewhere (if the -e option is given) or simply send it to stdout # (the default); otherwise be silent. email= while getopts e: opts do if [ "$opts" = e ]; then email=$OPTARG elif [ "$opts" = '?' ]; then echo "cron-harness: Error parsing options" >&2 exit 1 fi done shift $(($OPTIND - 1)) if [ -z "$*" ]; then echo "cron-harness: No command given" >&2 exit 1 fi outfile=`mktemp -t cron-harness.out.XXXXXXXXXX` if [ $? -ne 0 ]; then echo "cron-harness: Error creating temporary file" >&2 exit 1 fi exit=0 $* > $outfile 2>&1 if [ $? -ne 0 ]; then if [ -n "$email" ]; then hostname=`hostname | sed 's/\..*//'` mail -s "cron-harness: Error running $1 on $hostname" $email < $outfile else cat $outfile fi exit=2 fi rm -f $outfile exit $exit