Friday, January 4, 2008

scripting mailq output for error handling

So, I was in need of a way to parse mailq on a postfix box and remove all mail that was sitting there waiting indefinately because of an error. Since setting a timeout is not good when email deliverability is king, I had to do this manually.

I'm not a user of 'tr'.. or rather I wasn't. I so am now...

Here's the script:

mailq | tr '\n' '|' | sed "s/MYDOMAIN.COM|/MYDOMAIN.COM/g" | tr '|' '\n' | grep error | awk {'print $1'} | postsuper -d -


Here's what it does:

take the output of mailq and pipe it to tr
strip all newline chars and replace with pipes
hand this output to sed, and do a regex search and replace on MYDOMAIN.COM| and replace with MYDOMAIN.COM
handoff to tr again, and remove the remaining pipes, and replace those with newlines.
find the error
grab the first "word" in each line (the message id in this case) and pass the output to postsuper
postsuper will then delete the messages based on the message id.

The internet is held together with pipe and tubes. It's absolutely true.

No comments: