18

Is there a simple command to find out the current number of messages in the linux mail queue? mailq dumps out a verbose list, but it's not convenient for a quick overview.

I'm using Ubuntu and postfix.

danp
  • 399
  • 1
  • 2
  • 8

4 Answers4

30

If you just want to know the number of messages sitting in the deferred queue, then the following should get you a quick answer:

find /var/spool/postfix/deferred -type f | wc -l

There are three other queues. See http://www.porcupine.org/postfix/queueing.html for details.

Brian Showalter
  • 1,029
  • 9
  • 13
  • 1
    in my case it was this: `find /var/spool/mqueue -type f | wc -l` and this `find /var/spool/mqueue-rx -type f | wc -l` as I have two queues and don't use postfix. – Jeroen Wiert Pluimers Aug 16 '12 at 20:17
17

You could filter the output and display only the last line:

mailq | tail -n 1
Martin
  • 706
  • 3
  • 6
  • 2
    That's a nice idea, but the queue is massive, so it take's a long time to return a result. Anything faster..? – danp Aug 18 '10 at 13:38
  • If the queue is really really massive, there may be another issue (unless you're an ISP or mail hosting service). You shouldn't have a backup so huge that you have to wait a few minutes for these results...? – Bart Silverstrim Aug 18 '10 at 13:41
  • I'm sure there is another issue, but that'll come in another question ;) – danp Aug 18 '10 at 13:45
4

As a related matter, you can also obtain the number of messages in your mailbox stored in mbox format, by modifying Brian Showalter's suggestion using the command "mail --headers."  For example, I have this line in my .bashrc file:

if [ -s /var/mail/$(whoami) ] ; then echo -e "\nYou have $(ls -s -h /var/mail/$(whoami) | cut -d" " -f 1) of mail.  Number of messages: $(mail --file /var/mail/$(whoami) --headers | wc -l) ($(mail --file /var/mail/$(whoami) --headers | sed '/^>* *[0-9]/d' | wc -l) unread)" ; fi
Greg Marks
  • 141
  • 4
1

This is

find /var/spool/postfix/deferred -type f | wc -l

good idea, but it doesn't work if my Zabbix-Agent isn't run as a root. So I used this

NUM=`mailq | grep -E "Requests" | awk '{print $5}'`; if [ -z "$NUM" ]; then echo "0"; else echo $NUM; fi

for my own UserParameter.

Mareg
  • 11
  • 2