Sunday, May 08, 2011

Cheap Mutex with Fuser

On one box, I have a cronjob which runs every minute.  The cron task grabs a snapshot from a web cam, saves it in a monster tree, based on date and time, and then pushes it up to a larger 'mother ship' computer for it to process.  Don't get creeped out -- it's a time-laps movie of a building being built.

Anyway, it's that last rsync that sucks.  It's pushing stuff about 500mi away, and it's over SBC lines, so it's crunchy as hell.  Often, the VPN will die and take out not one but four sockets at once.  Of course, with cron, that means 4 email messages and one more every minute until that VPN comes back up.

So here's a cheap-ass mutex thing:

#!/bin/sh
lckfile=/tmp/foo
exec 2>${lckfile}
wget -O- http://www.cnn.com/
fuser ${lckfile}
echo $?

if ! fuser ${lckfile}
then
    fuser ${lckfile} > ${lckfile}
else
    touch /tmp/foot
fi
rm -f \
     /tmp/foop \
     /tmp/foot \
     ${lckfile}

fuser ${lckfile} ; echo $?

There.  This is a working demo I used to build the eventual thing, so the implementation varied as much as YMMV, but that's the guts of it.

Enjoy, kids. And yes, I enjoy foo.

Labels: , , , ,