a0f686bdc81f880de95a51386844b58da8818979
[blog.git] / posts / Abax / pbs_queues / file / qcleanmail
1 #!/bin/bash
2 #
3 # Running my qcmd implementation on your system mailbox can quickly fill it up
4 # with PBS junk.  This scans through the box and deletes any messages from the
5 # PBS admin.
6 #
7 # from man procmail:
8 # Procmail can also be invoked to postprocess an already filled system mailbox.
9 # ...
10 #
11 # This script is almost verbatim from 'man procmail'
12
13 ORIGMAILS="$MAIL $HOME/.mailspool/completed" 
14 FILTER=$HOME/.mailspool/mailfilter/qcleanmail.proc
15
16 clean ()
17 {
18     ORIGMAIL=$1
19     if cd $HOME && 
20         test -s $ORIGMAIL &&
21         lockfile -r0 -l1024 .newmail.lock 2>/dev/null
22         then
23         umask 077
24         trap "rm -f .newmail.lock" 1 2 3 13 15
25         echo "cleaning $ORIGMAIL" 
26         # lock the system maildir, and extract all the messages
27         lockfile -l1024 -ml
28         cat $ORIGMAIL >>.newmail &&
29         cat /dev/null >$ORIGMAIL
30         lockfile -mu
31         # lock released, now new messages can arrive as they normally do
32         
33         # process the mail we copied out with our filter
34         # mail that should be in $ORIGMAIL will be appended as procmail notices...
35         formail -s procmail $FILTER <.newmail &&
36           rm -f .newmail
37           rm -f .newmail.lock
38     fi
39     return 0
40 }
41
42 for M in $ORIGMAILS
43   do
44   clean $M
45 done
46
47 exit 0