Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

I'm using Centos and sendmail.

mailq -v | egrep -v '^-' | | sort | uniq -c
But the above command output is as following:

Above command did not fulfill my requirement, any help in this regard please.

Here is updated output:> has 5 message(s)> has 1 message(s)> has 2 message(s)> has 1 message(s)> has 1 message(s)
share|improve this question
up vote 2 down vote accepted

Well, if you are going to use perl, might as well go all the way.

The following is a fairly imperfect way of counting the number of messages per domain:


use strict;

my @mailq = `cat /home/users/rilindo/mailq`; #Had to simulate the output of the mail command here. Change this to the actual mailq path, e.g. /usr/bin/mailq
my %domains = ();

foreach my $m (@mailq) {
    $m =~ s/^\s+//;
    $m =~ s/\s+$//;
    $m =~ s/>//;
    next if $m =~ /Queue/;
    if ($m =~ /\d\d:\d\d:\d\d/) {
    else {

foreach my $d (keys %domains) {
    print $d . " has $domains{$d} message(s)" . "\n";

Essentially, we send an output of the mailq command into an array and iterate. For each record in the array, we strip the leading and trailing spaces/newlines, and then split at the "@" sign. Then we insert domain as a key (if it doesn't exist) and then increment it in a hash. On the next try, it will simply increment the value if the same domain was found. From there, we loop through the hash and then print out the domain with the total number of matches.

The result:

[rilindo@localhost ~]$ ./ has 6 message(s) has 2 message(s) has 2 message(s)

Like I said, its not perfect, but it does the job. If nothing else, it will give you an idea of where to go from here.

Incidentally, since it appears you know perl, review of Perl's hash data structures will prove to be very useful:


share|improve this answer
Can you please help me to remove " > " sign. Output is as following with each domain> has 2 message(s) – User4283 Sep 19 '11 at 13:57
Can you post the updated output? Let me see if I can work around it. – Rilindo Sep 19 '11 at 13:59
I've added updated output in my question. – User4283 Sep 19 '11 at 14:05
I added a new line to strip off the > sign. I am not sure if I need to escape it, so let me know if it didn't work or not. – Rilindo Sep 19 '11 at 14:09
Yes it is working :) Thanks Rilindo – User4283 Sep 19 '11 at 14:25

Try this:

# mailq -v | awk 'BEGIN { FS = "@" } \
!/^[a-zA-Z0-9-]|^[ \t]+(\(|\/|Total requests:)/ { print $2 }' | sort | uniq -c
share|improve this answer
sed and egrep are unnecessary since awk can also do the selection; alternatively, use grep -o to extract the email address domains and omit the sed and awk. – ramruma Sep 18 '11 at 21:06
Thanks for your comment. Could you please show us your solution with grep -o? – quanta Sep 19 '11 at 2:13
This is a very elegant solution and preferable to the Perl ones as some systems may not have perl installed (but everything that can pretend to be unix-like should have awk, sort and uniq) – voretaq7 Sep 19 '11 at 3:40
I agree. This will work as well. – Rilindo Sep 19 '11 at 14:29
mailq -v |grep -o '@.*' |sort |uniq -c |sort -nr – ramruma Sep 21 '11 at 3:15

"simple" is relative. mailq's output is a royal pain to parse, but it can be done. Typical mailq verbose output is something like:

-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
637F5CFF9C*    1497 Sat Dec 18 21:40:34 

637F5CFF9d*    1497 Sat Dec 18 21:40:35 

Some creative hackery can get you what you need:

First, you want to strip out that top line - it's useful to a human but not relevant to our parsing goals.
Easiest way: mailq -v | egrep -v '^-'

Now you want to grab the recipient information and extract the domain name. Perl is your friend here - Pipe the output through this handy script (let's call it

$/ = "\n\n";                                    # Use a blank line as the separator.
while (<>) {
        ($x,$recip) = split(/\n/, $_);          # Extract the recipient line
        ($user,$domain) = split(/@/, $recip);   # Get the domain.
        print "$domain\n";                      # Print Recipient Domain

This just leaves the easy part - counting the domains (pipe through sort | uniq -c).

So mailq -v | egrep -v '^-' | | sort | uniq -c will give you output something like:

share|improve this answer
it only print single domain. Output is as following. 1> – User4283 Sep 15 '11 at 18:16
Did you remember the -v flag to mailq (which I left out of my examples)? – voretaq7 Sep 15 '11 at 18:19
I've executed the same command. > mailq | egrep -v '^-' | | sort | uniq -c – User4283 Sep 15 '11 at 18:21
Read my comment again. Also see the most recent edit to the answer. – voretaq7 Sep 15 '11 at 18:24
Dear i've read and now executed the command " mailq -v | egrep -v '^-' | | sort | uniq -c " results are same with this command too. – User4283 Sep 15 '11 at 18:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.