[CentOS] use of MAILTO variable in crontab
Devin Reade
gdr at gno.org
Wed Jul 13 21:47:03 UTC 2011
If you want the output of your cronjob to go to a particular place
that is a bit too inconvenient to do using the built-in functionality,
put a script around your commands and handle the mailing yourself.
This is a good general rule whenever something you want to run from
cron is not trivial (in the sense of its invocation):
#! /bin/sh
MAILTO=somebody at example.com
COMMAND="/usr/local/bin/mycommand"
COMMAND_ARGS="some args"
[ -x $COMMAND ] || exit 0
# safe place for temporary files, including cleanup on exit
TDIR=`mktemp -qtd mycommand.XXXXXXXXXX`
if [ $? -ne 0 ]; then
echo "Failed to create temporary directory; aborted"
exit 1
fi
trap "/bin/rm -rf $TDIR" 0 1 2 15
OUT1=$TDIR/out1
MAIL_OUT=$TDIR/mail-out
# run it
$COMMAND $COMMAND_ARGS 2>&1 > $OUT1
# only mail something out if it produced output. Alternately
# you might want to check the value of $?
if [ -s $OUT1 ]; then
h=`hostname`
cat > $MAIL_OUT <<EOF
To: $MAILTO
Subject: mycommand problem on $h
There is a problem with mycommand on $h. The following is
the output of $COMMAND $COMMAND_ARGS
EOF
cat $OUT >> $MAIL_OUT
/usr/sbin/sendmail -t < $MAIL_OUT
fi
More information about the CentOS
mailing list