jtest - rotate your procmail log

Posted on October 29, 2007 
Filed Under Mail, Unix

I’m using GNU grep, which allows me to use the -B option to display a few lines of context before the line where it found a match, and -A to display lines after it. mailstat is a helper program which like formail is included in the procmail package. It automatically moves the log file you feed it to a new file with the same name plus the extension “.old”, so in this case, it moves “pm.log” to “pm.log.old”.

To use this conveniently, save it to a directory that is in your Unix “PATH” — I use $HOME/bin.


#! /bin/bash
#  jtest - review and purge a procmail log
# Save as $HOME/bin/jtest and make executable with:
#	$ chmod +x $HOME/bin/jtest
#-----------------------
# To see a line for each message as the log is purged, use this line:
# mailstat $HOME/pm.log  # remove the leading # to activate
# To just purge the log and rely on the summary, use this line instead:
mailstat $HOME/pm.log > /dev/null  # This line is active
# mailstat creates pm.log.old - if it's empty, that means no new mail:
if [ ! -s $HOME/pm.log.old ]; then
 echo "Nothing new."
 exit 0
fi
# To get a summary, scan the backed-up log with GNU grep:
grep -A 2 "From " $HOME/pm.log.old | grep -v Folder
echo " "
# Counters are good -- the backtick trick keeps everything together
# on the same line:
echo `grep "From " $HOME/pm.log.old | wc -l` "messages, "\
 `grep "/dev/null" $HOME/pm.log.old | wc -l` dropped
# Each run destroys the .old file, so let's save one more generation
# in case we want to review it in more detail later:
mv $HOME/pm.log.old $HOME/pm.log.older
touch $HOME/pm.log.old   # zero line file gives the "Nothing new." message
echo " "

Comments

Leave a Reply

You must be logged in to post a comment.