On Wed, 24 Jan 2007, Paul Pruett wrote:
> Using cron and atactl to email smartstatus errors
> to an email address other than cron user:
...
I use the following script to help with cron stuff, it can do
what you want.
-d
---------------------------
#!/bin/sh
# Helper for cron(8) to send mail only if command terminates abnormally.
# Also allows you to specify a different recipient.
usage()
{
echo "Usage: cronmail.sh [-h] [-r recipient] command [args...]" 1>&2
exit 1
}
args=`getopt hr: $*`
[ $? -ne 0 ] && usage
set -- $args
for o ; do case "$o" in
-h) usage;;
-r) RECIPIENT=$2; shift; shift;;
--) shift; break;;
esac ; done
# Need at least one argument (command)
[ -z "$1" ] && usage
OUTTMP=`mktemp -t cronmail.out.XXXXXXXXXXXXXXXX`
if [ $? -ne 0 ]; then
# Fall back to executing the command with unredirected output
exec $*
fi
$* >$OUTTMP 2>&1
RC=$?
if [ $RC -ne 0 ]; then
if [ -z "$RECIPIENT" ]; then
cat $OUTTMP
else
mail -s "Failed cron command $1" $RECIPIENT < $OUTTMP
fi
fi
rm $OUTTMP
exit $RC