[NBLUG/talk] Mutual exclusion in shell scripts?

Lincoln Peters sampln at sbcglobal.net
Sat Jan 20 16:07:33 PST 2007


I have the following shell script that I use to train spamassassin:

while [ -e /tmp/filter.lock ]
do
	sleep 10
done
touch /tmp/filter.lock
sa-learn $1 --no-sync
rm -f /tmp/filter.lock

Although multiple instances of the script may be called at the same time, I 
only want one to be running at a given time.  I do this because my mail 
server isn't powerful enough to handle multiple instances of sa-learn 
simultaneously.

If a second or two passes between calls to the script, everything seems to 
work OK.  However, if the script is called several times in rapid succession, 
e.g.:

filter.sh < msg1 & filter.sh < msg2 & filter.sh < msg3

Enough time passes between checking for /tmp/filter.lock and actually 
setting /tmp/filter.lock that every instance of the script runs 
simultaneously.  Is there a way to avoid that?


-- 
Lincoln Peters		<sampln at sbcglobal.net>

Many a man who thinks he's going on a maiden voyage with
a woman finds out later that it was just a shake-down cruise.



More information about the talk mailing list