Display number of messages in linux mail queue

119,165

Solution 1

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.

Solution 2

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

mailq | tail -n 1

Solution 3

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

Solution 4

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.

Share:
119,165

Related videos on Youtube

dmp
Author by

dmp

Updated on September 17, 2022

Comments

  • dmp
    dmp almost 2 years

    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.

    • user1364702
      user1364702 almost 14 years
      What mail transfer agent are you using?
    • Prix
      Prix almost 14 years
      like bart said, tell us what MTA youre using so we can give you a more specific and correct approch.
    • dmp
      dmp almost 14 years
      sendmail, sorry.
    • user1364702
      user1364702 almost 14 years
      Sure it's sendmail, not postfix? Just making sure...
    • dmp
      dmp almost 14 years
      good catch, its postfix not sendmail.
  • dmp
    dmp almost 14 years
    That's a nice idea, but the queue is massive, so it take's a long time to return a result. Anything faster..?
  • user1364702
    user1364702 almost 14 years
    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...?
  • dmp
    dmp almost 14 years
    I'm sure there is another issue, but that'll come in another question ;)
  • Jeroen Wiert Pluimers
    Jeroen Wiert Pluimers almost 12 years
    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.
  • Scottie H
    Scottie H almost 4 years
    Just what I was looking for. Thanks!