How to Check Postfix Mail Queue Message Count in Linux: Quick Command Guide


2 views

When working with Postfix on Linux systems like Ubuntu, administrators often need to quickly check the number of messages currently in the mail queue. While the standard mailq command provides detailed output, it's not optimized for quick status checks.

For a quick count of messages in the Postfix queue, use:

postqueue -p | grep -c '^[0-9A-F]'

This command pipeline:

  • Uses postqueue -p (equivalent to mailq)
  • Filters output with grep -c to count lines
  • Matches queue IDs (starting with hex characters)

For systems using older Postfix versions:

mailq | grep -c '^[0-9A-F]'

To see both queued and deferred messages:

find /var/spool/postfix/{deferred,active,incoming} -type f | wc -l

For regular monitoring, save this bash script as check_mailq.sh:

#!/bin/bash
QUEUE_COUNT=$(postqueue -p | grep -c '^[0-9A-F]')
echo "Mail queue count: $QUEUE_COUNT"
if [ $QUEUE_COUNT -gt 50 ]; then
    echo "Warning: High queue count detected"
fi

Typical queue counts:

  • 0-10: Normal operation
  • 10-50: Possible temporary delay
  • 50+: Potential mail delivery issues

For detailed breakdown by domain:

postqueue -p | awk '/^[0-9A-F]/ {print $7}' | \
    awk -F@ '{print $2}' | sort | uniq -c | sort -rn

When administering Postfix mail servers, frequently checking the queue status is crucial. While mailq or postqueue -p provides detailed output, these commands often show more information than needed for simple monitoring.

For a quick count of messages in the Postfix queue:

postqueue -p | grep -c '^[0-9A-F]'

This pipeline:

  • Uses Postfix's native postqueue -p (equivalent to mailq)
  • Filters output with grep to count lines starting with message IDs
  • Returns just the numeric count

For systems without grep or when needing Perl-compatible regex:

postqueue -p | perl -nle 'print scalar grep(/^[0-9A-F]{10,}/, $_)'

Or using qshape (Postfix-specific tool):

qshape -s 1 active | wc -l

For regular monitoring, create a bash script:

#!/bin/bash
QUEUE_COUNT=$(postqueue -p | grep -c '^[0-9A-F]')
echo "Mail queue contains $QUEUE_COUNT messages"
exit $QUEUE_COUNT

Typical queue counts:

  • 0-10: Normal operation
  • 10-100: Potential delays
  • 100+: Likely delivery issues

To log queue size hourly:

0 * * * * /usr/bin/postqueue -p | grep -c '^[0-9A-F]' >> /var/log/mailq.log