linuxshellcroneximmail-queue

Linux shell script to remove 2 day old frozen emails from exim queue


I want to put shell script under the cron job which will do following:

1) Removes 2 days old email messages which are in the Exim queue and are bounced/frozen messages, which won't be delivered. Script must not remove messages which are legitimate and are just waiting their time to be delivered.

2) Removes messages which are from invalid sender like null or <> and / or sent to invalid recipient nobody or <>

Thanks a lot.


Solution

  • exiqgrep is your friend! You can easily find queue items matching certain criteria and pipe the found message-id:s to a remove command. E.g:

    exiqgrep -z -o 172800 -i | xargs -r exim -Mrm 
    

    which translates as follows: find queue items (exiqgrep) that are frozen (-z) and older than two days (-o 172800) and output their message id:s (-i) to xargs that runs only if getting any input (-r) telling exim to remove (-Mrm) the items with given message id:s.

    Null (or <>) senders are certainly not invalid! Bounces and other (non-)delivery reports are typically sent from a null address in order to avoid infinite loops in case the bounce is not deliverable. However if you have lots of these on the queue and they stay there for long (e.g. if you are trying to bounce spam sent from fake addresses) you can certainly clean them up too. For example:

    exiqgrep -o 86400 -f '<>' -i | xargs -r exim -Mrm
    

    which finds queue items older than one day with a null sender and deletes them.

    EDIT: you could also set the following option in your exim.conf to automatically remove frozen bounces after two days:

    ignore_bounce_errors_after = 2d