
mailq Command in Linux
The mailq command in Linux is a powerful tool used to display the mail queue of the mail transfer agent (MTA) on your system. This command is particularly useful for system administrators who need to monitor, troubleshoot, and manage email delivery issues.
Table of Contents
Here is a comprehensive guide to the options available with the mailq command −
- Understanding mailq Command
- Syntax of mailq Command
- mailq Command Options
- Examples of mailq Command in Linux
- Advanced Features of mailq Command
Understanding mailq Command
The mailq command is part of the mailutils package, which provides a suite of utilities for handling email. It allows you to view the status of emails in the mail queue, including those that are pending delivery, deferred, or experiencing delivery issues. This command is essential for maintaining the health of your email system and ensuring that emails are delivered promptly.
Syntax of mailq Command
The basic syntax for the mailq command is −
mailq [options]
Without any options, the command displays the current status of the mail queue.
mailq Command Options
Here are some of the most commonly used options with the mailq command −
Options | Description |
---|---|
-qI [queue_id] − | Process a specific email in the queue by its queue ID. |
-qR [recipient] − | Process emails in the queue for a specific recipient. |
-q − | Process the mail queue. |
-v − | Verbose mode. Display detailed information about each email in the queue. |
-qS [sender] − | Process emails in the queue from a specific sender. |
-qf − | Force processing of the mail queue. |
-bp − | Display the mail queue (equivalent to mailq). |
-Ac − | Use the alternate configuration file. |
-Am − | Use the alternate mailer configuration file. |
Examples of mailq Command in Linux
Let's explore some practical examples to understand how to use the mailq command effectively.
Display the Mail Queue
This command displays the current status of the mail queue. The output includes information about each email in the queue, such as the queue ID, sender, recipient, and status −
mailq

Verbose Mode
This command displays detailed information about each email in the queue, including the reason for any delivery issues. The -v option enables verbose mode −
mailq -v

Process the Mail Queue
This command processes the mail queue, attempting to deliver any pending emails. The -q option forces the MTA to process the queue immediately −
mailq -q

Process a Specific Email by Queue ID
Replace QUEUE_ID with the actual queue ID of the email you want to process. This command processes a specific email in the queue by its queue ID −
mailq -qI QUEUE_ID
Process Emails for a Specific Recipient
This command processes emails in the queue for a specific recipient. The -qR option specifies the recipient's email address −
mailq -qR recipient@example.com

Process Emails from a Specific Sender
This command processes emails in the queue from a specific sender. The -qS option specifies the sender's email address −
mailq -qS sender@example.com

Force Processing of the Mail Queue
This command forces the MTA to process the mail queue, attempting to deliver any pending emails regardless of their current status. The -qf option forces processing −
mailq -qf

Display the Mail Queue Using -bp
This command displays the mail queue, equivalent to running mailq without any options. The -bp option is an alias for displaying the mail queue −
mailq -bp

Use an Alternate Configuration File
Replace /path/to/config.cf with the path to the alternate configuration file. This command uses the specified configuration file for processing the mail queue −
mailq -Ac /path/to/config.cf

Use an Alternate Mailer Configuration File
Replace /path/to/mailer.cf with the path to the alternate mailer configuration file. This command uses the specified mailer configuration file for processing the mail queue −
mailq -Am /path/to/mailer.cf

Advanced Features of mailq Command
In addition to the basic options and examples, the mailq command provides advanced features that can be useful in specific scenarios. Let's explore some of these features with practical examples.
Automating Mail Queue Processing with Cron Jobs
This cron job processes the mail queue every hour. The sendmail -q command forces the MTA to process the mail queue, ensuring that emails are delivered promptly −
0 * * * * /usr/sbin/sendmail -q

Using mailq in Shell Scripts
This shell script checks if the mail queue is empty and prints a message based on the result. The mailq command is used to display the mail queue, and grep is used to search for the "No mail in queue" message −
#!/bin/bash if mailq | grep -q "No mail in queue"; then echo "Mail queue is empty" else echo "Mail queue is not empty" fi

Filtering Emails in the Queue
This command filters the mail queue to display only emails for a specific recipient. The grep command is used to search for the recipient's email address in the mail queue −
mailq | grep "recipient@example.com"

Monitoring the Mail Queue
This command monitors the mail queue, refreshing the display every 60 seconds. The watch command is used to run mailq at regular intervals, allowing you to keep an eye on the mail queue in real-time −
watch -n 60 mailq

Analyzing Mail Queue Statistics
This command analyzes the mail queue and displays statistics about the number of emails for each queue ID. The awk, sort, and uniq commands are used to process and analyze the mail queue data −
mailq | awk '{print $1}' | sort | uniq -c | sort -nr

Clearing the Mail Queue
This command clears the mail queue by deleting all emails. The postsuper -d ALL command is used to delete all emails in the queue. Use this command with caution, as it will remove all pending emails −
postsuper -d ALL

Resending Deferred Emails
This command resends all deferred emails in the mail queue. The postsuper -r ALL command is used to requeue all deferred emails for delivery −
postsuper -r ALL

Viewing Detailed Information About a Specific Email
Replace QUEUE_ID with the actual queue ID of the email you want to view. This command displays detailed information about a specific email in the queue. The postcat -q command is used to view the email's content and headers −
postcat -q 1

Conclusion
The mailq command is an essential tool for anyone working with Linux systems, providing a simple and effective way to monitor, troubleshoot, and manage the mail queue.