Solved

Automatic clean up of var/spool/mqueue

Posted on 2014-01-19
5
2,338 Views
Last Modified: 2014-01-19
What are files in the /var/spool/mqueue and what are its purpose? Please create me a one-liner script to regular delete these files if not needed in the cron
0
Comment
Question by:oo_tatang
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:bevhost
ID: 39792744
Create a file in /etc/cron.monthly (or weekly, daily) which contains...
/bin/rm -f /var/spool/mqueue/*
0
 
LVL 19

Expert Comment

by:bevhost
ID: 39792745
These are actually mailboxes.  to read the mail you can type mail at the prompt to read the mail for the user you are logged in as. man mail for help.
Most mail for root comes from cron jobs that run.
0
 
LVL 9

Accepted Solution

by:
gt2847c earned 500 total points
ID: 39792873
The files in /var/spool/mqueue are mail messages queued to be sent.  If you just kill all the files in that directory, you may be wiping out legitimate mail that hasn't been sent either due to delivery problems, or just hasn't been picked up by the mail program.  Most mail senders (sendmail, postfix, others) have a configured max retry time for attempts to resend previously undelivered mail (5 days is a common setting).  So if you want to clear out the entries in this directory that are most likely dead mail, you should kill files older than your max retry time.

A one liner that can be added to a Cron job would be:

/usr/bin/find /var/spool/mqueue -mtime +6 -exec /bin/rm -f {} \;

Open in new window


That will find anything in the /var/spool/mqueue directory that hasn't been modified for 6+ days and remove it.

You can either add to a cron file  as mentioned by bevhost above or you can simply create a cron table entry for an account that has sufficient privileges (root):

crontab -u root -e

Open in new window

and then add the line:
0 0 * * * /usr/bin/find /var/spool/mqueue -mtime +6 -exec /bin/rm -f {} \;

Open in new window

which will run midnight every night to clear out old files from the directory.
0
 

Author Closing Comment

by:oo_tatang
ID: 39793040
The files are owned by root:smmsp . What should I do now? Do I need to regularly delete or reduce hold time to +4 or how will i know these mail messages are important since the server is just a cell manager for backups
0
 
LVL 9

Expert Comment

by:gt2847c
ID: 39793167
You can adjust the -mtime parameter to suit your needs, but it should be configured to be longer than the maximum retry/queue time for your system mailer.  That said, the mailer program on your machine should, if it's running properly, remove the queued message files once a message is successfully delivered.  You shouldn't have to clean up the /var/spool/mqueue directory very often yourself unless there is a problem.  If your system is stacking up large numbers of files in the mail queue directory, you probably need to find out why.  

As to what the messages are, and why they're there, you may need to look at the files there and see what's in them. Many *nix systems use mail to notify of job completion, status, failures, etc.  Mail from automated systems that don't have mailboxes (i.e. sender address is not a valid local or remote account), that get bounced (invalid recipient address) may end up being left behind in the mqueue directory.  Those would need to be cleaned up, but before just killing them all off, you should try and determine what it is that is sending mail to non-existent recipient (assuming that's the problem) so that it can either be configured correctly or stopped from sending.

Be advised, that depending on who owns the server and company policy, you might need to get permission before poking through mail files (just an FYI - some HR folks get upset about this especially if there are any sensitive messages floating about).
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question