SpamAssassin has a feature to "learn" spam. Each user has a .spam folder in their mail folder and they will be instructed to move any spam into that folder. (Users are using IMAP.) I have set up a cron job to feed the contents of each account's .spam folder into sa-learn.
02 02 * * * sa-learn -p ~/.spamassassin/user_prefs --spam ~/mail/*/*/.spam/{cur,new}
This works as intended, according to the cron confirmation email.
I now wish to autodelete the contents of the .spam folders on successful completion of the sa-learn command. Am I correct in thinking I can add the following onto the end of the above cron task?
&& rm ~/mail/*/*/.spam/{cur,new}
Does this look correct?
Notes: this is being tested for use on multiple cPanel reseller hosting acounts. I do not have shell access.
Update 1 after Celada's answer and comments below:
The way you need to do this is to atomically move each message to a different directory which you control and which the IMAP server doesn't touch, then process and delete it from within that location. Fortunately you are using Maildir which permits such atomic moves.
How about
mkdir -p /tmp/sa_tmp
&& mv ~/mail/*/*/.spam/{cur,new} /tmp/sa_tmp
&& sa-learn -p ~/.spamassassin/user_prefs --spam /tmp/sa_tmp
&& rm -rf /tmp/sa_tmp
I'm deliberately not using mktemp
so that if a directory gets left behind it will be deleted on the next run.
Update 2 after xhienne's comment:
Not sure moving the directories cur and new is a good idea => move their content instead. Moreover, there will be a name collision since you are moving many directories named new and cur to the same destination.
I'm not sure how to avoid filename collisions.
Finally, moving to /tmp might mean moving to another partition, with the incurred extra-I/Os and the possibility of lacking disk space (/tmp is sometimes a small separate partition) => preferably, move to a special folder like ~/mail/.sa-learn that you can delete in the end
mkdir -p ~/mail/.sa-learn
&& mv ~/mail/*/*/.spam/{cur,new}/* ~/mail/.sa-learn
&& sa-learn -p ~/.spamassassin/user_prefs --spam ~/mail/.sa-learn
&& rm -rf ~/mail/.sa-learn
Update 3:
I think this doesn't work if the new
folder doesn't exist. I'm getting error:
mv: cannot stat `/home/username/mail/*/*/.spam/new/*': No such file or directory
There don't seem to be any "ignore" switches for mv
. Any ideas?
cur
andnew
is a good idea => move their content instead. Moreover, there will be a name collision since you are moving many directories namednew
andcur
to the same destination. Finally, moving to/tmp
might mean moving to another partition, with the incurred extra-I/Os and the possibility of lacking disk space (/tmp
is sometimes a small separate partition) => preferably, move to a special folder like~/mail/.sa-learn
that you can delete in the end.