====== redirect_mail =====
This script will send all system emails located in /var/spool/mail/, preventing this folder from growing, and alert the concerned team of problems, most time related to bad scripts started via crontabs.
Environment variables
[root@nim]/root/scripts/bin# cat .env
# global variables for all scripts
export binpath=/root/scripts/bin
export sn=`basename $0 | cut -d. -f1`
export HOSTNAME=`/usr/bin/hostname -s`
export logpath=/root/scripts/logs
export logname=$logpath/$sn.log
export configfile=$binpath/$sn.cfg
Config file, for each system user associate an email adress. The **master_destination** will alert if a mailbox exists on the server (in /var/spool/mail/) and is not defined in .cfg file
[root@nim]/root/scripts/bin# cat redirect_mail.cfg
# 1° colonne = user-name
# 2° colonne = addresse e-mail ou "ignore"
# master_destination myemail2@emailserver.lu
root myemail1@emailserver.lu
oracle oracle@emailserver.lu,oracle-grp@emailserver.lu
test ignore
The script will forward all system mail to a destination defined in .cfg file
[root@nim]/root/scripts/bin# cat redirect_mail.sh
#!/usr/bin/ksh93
#@(#) send mail to user assigned mailbox
#############################################
# Check in /var/spool/mail and send mail to the appropriate person
# and purge the mailbox
# version 1.0 16-02-2017 manu
#############################################
dir=`dirname $0`
. $dir/.env
dest="$mail_system"
mail_addressbook=$configfile
#------------------------------------------------
send_mail ()
{
set -x
mail_address=$1
flen=$(ls -l $file | awk '{print $5}')
if [ $flen -gt 1048576 ]
then
# this is a big mail and will be split in 1MB part
split -b 1m $file ${file}_split_
ctr=0
nb=$(ls ${file}_split_* | wc -l);(( nb = $nb + 0 ))
for pj in $(ls ${file}_split_* )
do
(( ctr = $ctr + 1 ))
uuencode $pj $pj.log | mail -s "Mail from user $file on $(hostname) part ${ctr}/${nb}" ${mail_address}
rm $pj
done
else
# This is a thin mail and will be directly send
uuencode $file $file.log | mail -s "Mail from user $file on $(hostname)" ${mail_address}
fi
shift 1
mail_list=$*
}
#############################################
# main
#############################################
main()
{
set -x
cd /var/spool/mail
ls -l
for file in $(ls)
do
mail_list=$(cat $mail_addressbook | grep -v "^#" | grep "^$file " | grep -v ignore | cut -f2- -d" ")
if [ ! -s $file ]
then
# file has a size 0 and will be deleted
rm $file
continue
fi
if [ "$mail_list" ]
then
echo $mail_list | while [ "$mail_list" ]
do
send_mail $mail_list
done
[ "$1" = "PURGE" ] && rm $file
else
# address not found: send request to deal with the user
ignore_me=$(cat $mail_addressbook | grep -v "^#" | grep "^$file " | grep ignore)
if [ ! "$ignore_me" ]
then
# Address not found, send mail to configure or ignore user
mail_list=$(cat $mail_addressbook | grep "master_destination" | tail -1 | sed 's/#//g' | awk '{print $2}')
echo "Please configure user $file on $(hostname) in $mail_addressbook" | mail -s "Unknown mail address $file on $(hostname)" ${mail_list}
fi
fi
done
# transfert queued mail stored in /var/spool/mqueue try : mailq
# sendmail -q
}
main "PURGE" > $logname 2>&1
Now you can add this script in the root crontab using **crontab -e**:
[root@nim]/root/scripts/bin# crontab -l
5 10 * * * /root/scripts/redirect_mail.sh >/dev/null 2>&1