#!/bin/bash #-----------------------------------------------------------------------# # SQG - Sendmail Queue Groomer # # David G. Sullivan # dsullivan@grapevinesolutions.com # 12/28/2003 # # Modified for Redhat - Linux / bash # # Alan Madill # amadill@hwy16.com # 08/11/2009 # # Description: # A very simple script to keep normal sendmail queues clear of # undeliverable email. Undeliverable email is moved to a separate # queue (morgue-queue) for processing from cron. Mail in the # morgue-queue is removed after X attempts at delivery. # # # Add the below command in cron to run every 30 minutes to process mail # in the morgue directory with a separate queue runner. # # /usr/lib/sendmail -q -O QueueDirectory=/var/spool/morgue-queue #-----------------------------------------------------------------------# #-----------------------------------------------------------------------# # Operating parameters #-----------------------------------------------------------------------# declare -i queue_max=10 # Max delivery attempts for normal mail before # moving mail to morgue directory. declare -i morgue_max=15 # Max delivery attempts for morgue mail before # deleting mail message. #-----------------------------------------------------------------------# # Command locations #-----------------------------------------------------------------------# SENDMAIL=/usr/lib/sendmail DATE=/bin/date GREP=/bin/grep CUT=/bin/cut MV=/bin/mv RM=/bin/rm LS=/bin/ls #-----------------------------------------------------------------------# # Directory locations #-----------------------------------------------------------------------# Mail_Queues=/var/spool/mqueue Mail_Morgue=/var/spool/morgue-queue #-----------------------------------------------------------------------# # Log file for morgue transactions (Mail delivery is still in syslog) #-----------------------------------------------------------------------# logfile=/var/log/morgue.log #-----------------------------------------------------------------------# # Stop sendmail #-----------------------------------------------------------------------# /sbin/service sendmail stop /bin/sleep 5 #-----------------------------------------------------------------------# # Check Mail Queues #-----------------------------------------------------------------------# for y in $($LS $Mail_Queues|$GREP ^qf*) do #echo "file" $y declare -i delv_attempts=$($GREP ^N $Mail_Queues/$y|$CUT -c 2-5) msgid=$(echo $y|$CUT -c 3-35) date=$($DATE) To=$($GREP ^rRFC822 $Mail_Queues/$y|awk '{print $2}') if (test $delv_attempts -ge $queue_max) then echo $date", MSGID: "$msgid" Moved, "$To >> $logfile #echo $MV $Mail_Queues"/qf"$msgid" "$Mail_Morgue $MV $Mail_Queues/qf$msgid $Mail_Morgue #echo $MV $Mail_Queues"/df"$msgid" "$Mail_Morgue $MV $Mail_Queues/df$msgid $Mail_Morgue fi done #-----------------------------------------------------------------------# # Check Morgue Mail Queue #-----------------------------------------------------------------------# for y in $($LS $Mail_Morgue|$GREP ^qf*) do declare -i delv_attempts=$($GREP ^N $Mail_Morgue/$y|$CUT -c 2-5) msgid=$(echo $y|$CUT -c 3-35) date=$($DATE) To=$($GREP ^rRFC822 $Mail_Morgue/$y|awk '{print $2}') if (test $delv_attempts -ge $morgue_max) then echo $date", MSGID: "$msgid" Removed, "$To >> $logfile #echo $RM $Mail_Morgue"/qf"$msgid $RM $Mail_Morgue/qf$msgid #echo $RM $Mail_Morgue"/df"$msgid $RM $Mail_Morgue/df$msgid fi done #-----------------------------------------------------------------------# # Start sendmail #-----------------------------------------------------------------------# /sbin/service sendmail start ############################################################################## ### This script is submitted to BigAdmin by a user of the BigAdmin community. ### Sun Microsystems, Inc. is not responsible for the ### contents or the code enclosed. ### ### ### Copyright Sun Microsystems, Inc. ALL RIGHTS RESERVED ### Use of this software is authorized pursuant to the ### terms of the license found at ### http://www.sun.com/bigadmin/common/berkeley_license.jsp ##############################################################################