11

How can I get number of messages count in Postfix's mailq? mailq command display entire mails in mailq but i would like to know count only.Is there any command for this. My OS is centos 5.5.

5 Answers5

21

I use this:

mailq | grep -c "^[A-F0-9]"

You can pipe the output of mailq through various other filters such as uniq, sort and wc to get other statistics.

Ladadadada
  • 26,337
  • 7
  • 59
  • 90
  • This is not accurate. When the Postfix queue is empty, then run 'mailq', we will get "Mail queue is empty" which also match "^[0-9A-Z]". And in such a situation it is not accurate. – andy Jul 07 '15 at 12:59
  • 1
    A better regex is `^[A-F0-9]` which correctly results in 0 when the mail queue is empty. I've edited the answer. – Ladadadada Oct 06 '15 at 16:26
  • 1
    How silly, the last line of the output is the count – Geoffrey Jan 02 '17 at 23:37
16

either mailq | tail -n 1 or find /var/spool/postfix/deferred -type f | wc -l

both works

replay
  • 3,240
  • 14
  • 17
  • 1
    I get "682430 Kbytes in 26472 Requests." for the first one and "23" for the second one. – rob Dec 18 '17 at 13:28
  • 1
    The second one only shows deferred messages, adding the active queue the counts should match up: `find /var/spool/postfix/active /var/spool/postfix/deferred -type f | wc -l`. – royarisse Nov 30 '21 at 08:50
0

Pipe the ouput to wc, if further refining is required, use grep.

Roman
  • 3,907
  • 3
  • 21
  • 34
0

I use

/usr/sbin/postqueue -p | /usr/bin/tail -n1 | /usr/bin/gawk '{print $5}'

seems quicker than grep but no 0 returns

Stavros
  • 101
0

I use:

mailq | grep -c "^$"

This is essentially the same as @Ladadadada's answer of:

mailq | grep -c "^[A-F0-9]"

Except I am just looking for the empty lines which follow each message entry in the mailq log, rather than something that looks like the start of a valid message-ID. It is therefore better performance-wise, although the difference is only noticeable on very a-typical mailq log sizes.

hiburn8
  • 101
  • 1