Today is: 19 January, 2012
Check todays hot topics

Gangster Bash Script to clean large sendmail queue's

This is a script a friend and I wrote while working at an ISP years ago. This script has been the subject of an inside joke of ours for a long time. We got tired of receiving several mailq alerts a day. After being beat down by the management (pertaining to why we needed to run open spam relays...) we accepted our fate and automated something up.

This script moves the mail queue to something completely new and random, starts a separate queue runner for it and restarts sendmail with a fresh empty queue. It's about as ghetto as they come.

#!/bin/bash
LOGIN=$1
FILENAME="new"$RANDOM".mqueue"
MSG="Queue script ran by `whoami`@`hostname` at `date` on $LOGIN. Created file /var/spool/$FILENAME and /var/spool/cleaner/$FILENAME.txt"
COMMAND="killall sendmail && cd /var/spool && mv mqueue $FILENAME && mkdir
mqueue && mkdir mqueue/q && chown -R root:mail mqueue && /usr/sbin/sendmail
-oQ/var/spool/$FILENAME -q && /etc/init.d/sendmail start && echo 'rm -rf
/var/spool/$FILENAME && rm -f /var/spool/cleaner/$FILENAME.txt' >
/var/spool/cleaner/$FILENAME.txt && at now + 2 days -v <
/var/spool/cleaner/$FILENAME.txt && echo '$MSG' > msg && mail -v -s 'Queue Script
ran for $LOGIN' -c <a href="mailto:name1@domain.com">name1@domain.com</a> <a href="mailto:name2@domain.com">name2@domain.com</a> < msg && rm -f msg"
 
if [ $LOGIN = "" ]; then
  echo "Login incorrect"
else
  echo "Running mail cleaner at $LOGIN"
  ssh -x $LOGIN $COMMAND
fi