Prerequisites
Before monitoring mail queue, make sure you have:
- Postfix installed
- Root or sudo privileges
Step 1: Check Mail Queue Size
Connect to your VPS:
ssh hxroot@YOUR_SERVER_IP -p 22
mailq | tail -n1
Or:
postqueue -p | wc -l
Step 2: Create Monitoring Script
sudo nano /usr/local/bin/check-mailq.sh
#!/bin/bash
THRESHOLD=50
MAILQ_COUNT=$(postqueue -p | grep -c "^[A-F0-9]")
if [ $MAILQ_COUNT -gt $THRESHOLD ]; then
echo "Mail queue has $MAILQ_COUNT messages (threshold $THRESHOLD)" | mail -s "Mail Queue Alert" admin@example.com
fi
sudo chmod +x /usr/local/bin/check-mailq.sh
Step 3: Schedule Cron Job
sudo crontab -e
*/15 * * * * /usr/local/bin/check-mailq.sh
Step 4: Monitor with Nagios/Other (Advanced)
Nagios users can use check_mailq plugin.
Step 5: View Detailed Queue Information
postqueue -p
To see why messages are stuck:
sudo tail -f /var/log/mail.log
✅ Mail queue monitoring active. Alerts will be sent if queue exceeds threshold.