Changing who gets email from cron entries?
David Tonhofer, m-plify S.A.
d.tonhofer at m-plify.com
Thu Oct 20 15:54:15 UTC 2005
Time for a script maybe? Here's one which may be of use.
mailwrap.pl:
#!/bin/sh
#################################################################################
# This script does the following:
#
# - Executes a command in a subshell (command passed as first argument).
# - Captures the stdout/stderr of the command.
# - Mail the result with the subject passed as second argument with 'OK'
# appended if the returnvalue was 0, with 'PROBLEM: RETURNVALUE X' appended
# if the returnvalue was not 0 but X
#
# The intended mail recipient is given as third argument
#
# This script is 'silent' so that it can be started from crontab without needing
# to redirect stdout and stderr to /dev/null; any errors in the script itself
# can thus be seen if cron mails them
#
# So, to summarize, pass the following arguments:
#
# 1) Command to execute in a subshell
# 2) Subject to use in an outgoing mail (OK or PROBLEM will be prepended)
# 3) Recipient of the mail as third argument
# 4) If you do NOT want to send messages if the returnvalue is 0, i.e. if
# you want to suppress the OK messages, pass NOOK as 4th or 5th argument
# 5) If you do not want to have empty messages on OKs, pass NOEMPTYOK as
# 4th or 5th arg
#################################################################################
# $Id: mailwrap.sh,v 1.5 2004/10/22 10:32:53 root Exp $
#################################################################################
if [[ -z "$1" ]]
then
printf "You have to pass the command to execute as first argument!\n" >&2
exit 1
fi
COMMAND=$1
if [[ -z "$2" ]]
then
printf "You have to pass the subject to use as second argument!\n" >&2
exit 1
fi
SUBJECT=$2
if [[ -z "$3" ]]
then
printf "You have to pass the recipient to use as third argument!\n" >&2
exit 1
fi
RECIPIENT=$3
if [[ "NOOK" == "$4" || "NOOK" == "$5" ]]
then
# printf "NOOK passed as fourth argument - will suppress OK message\n" >&2
NOOK=1
else
NOOK=0
fi
if [[ "NOEMPTYOK" == "$4" || "NOEMPTYOK" == "$5" ]]
then
# printf "NOEMPTYOK passed as fourth argument - will suppress empty OK message\n" >&2
NOEMPTYOK=1
else
NOEMPTYOK=0
fi
#
# Execute the passed command in a subshell. Note that /var/log/tmp must be accessible
# We use the process number to get somewhat unique filename. The /var/log/tmp
# directory should really not be writeable by anyone other than root. This should be
# checked here...
#
TMPFILE=/var/log/tmp/result.$$
$1 > $TMPFILE 2>&1
RESULT=$?
# Tweak the subject depending on $? (return value of last foreground pipeline)
if [[ $RESULT -eq 0 ]]
then
SUBJECT="$SUBJECT: OK"
else
SUBJECT="$SUBJECT: PROBLEM, returnvalue=$RESULT"
fi
# If NOEMPTY and the result is OK do not send anything
if [[ ($RESULT -eq 0) && ($NOEMPTYOK -eq 1) && !(-s $TMPFILE) ]]
then
rm $TMPFILE
exit 0
else
# Mail results
if [[ ! (($RESULT -eq 0) && ($NOOK -eq 1)) ]]
then
mail "$RECIPIENT" -s "$SUBJECT" < $TMPFILE
# else
# printf "Did not mail anything\n"
fi
# Delete the TMPFILE
rm $TMPFILE
# Pass the returncode of the called command to the caller
exit $RESULT
fi
--On Tuesday, October 18, 2005 9:38 AM -0400 Bill Tangren <bjt at aa.usno.navy.mil> wrote:
>> However, I'd like to be able to control which cron jobs email where. For
>> example, lets say I want script1 to send e-mail to bob at company.com and
>> logwatch to email security at company.com.
More information about the redhat-list
mailing list