Email queue alert for cPanel server via email

You need to monitor your email queue for your cPanel server for possible outgoing spam as well as to maintain the server performance. Often large email queue cases server overloading issue. It is very easy to get an alert if your email queue contains more than xxx emails. You can use the following code to monitor your email queue for your cPanel server:

#!/bin/bash

EMAIL="your email address"

MQUEUE=`find /var/spool/exim/input -name '*-H' | wc -l | sed -e "s/ //g"`

if [ $MQUEUE -gt XXX ]; then
echo "Mail queue at `hostname` has $MQUEUE messages!!!" | mail -s "MAIL QUEUE ALERT: Mail queue for `hostname`" $EMAIL
fi

In the above code replace XXX with the amount of emails. For example, if you wish to receive an alert if your email queue has more than 200 emails, replace XXX with 200. Save the above code in .sh file and allow execute permission to that file.

Once you save your file (make sure you save in .sh file like mailqueuealert.sh), you just need to add it in your cronjob. If you wish to monitor in every 10 minutes, you can set the following in your cronjob:

*/10 * * * * /path/to/your/above/script > /var/log/mailqueue_alert.log 2>&1

If you are unsure how to edit cronjob, please refer our post on edit/remove cronjob.

Leave a Reply