This is a simple shell script I wrote a long time ago which
is invoked by a CGI script on a local web page and notices
files that show up in a queue directory.
#!/bin/bash
# This script is started by the setuid program start-queue so that the
# CGI script which makes queue entries can have the queue processed
# as the right user.
# If the script detects a copy of itself already running, it exits and
# allows the existing copy to process the new queue entries when it gets
# around to it.
mydir=`dirname $0`
PATH=`$mydir/echo-path`
export PATH
cd $mydir/../queue
if [ -f ".pid" ]
then
exit 0
fi
echo $$ > .pid
mypid=`cat .pid`
if [ "$$" = "$mypid" ]
then
# Looks like this instance got here first, start processing the queue
trap "rm -f .pid" EXIT
while true
do
nextqueue=`ls -1 2>/dev/null | head -1`
if [ -f "$nextqueue" ]
then
handle-queue-entry "$nextqueue"
rm -f "$nextqueue"
else
# We seem to have run out of queue entries to process. Exit now.
exit 0
fi
done
fi