Myon wrote another blog post about cool unix features, this time about flock:
This is definitely of interest to me, as I’ve written some shell scripts recently which use a manual lock implementation which I’m not sure if it works as I’d intended.
For example, here’s a shell script I wrote to be run when incron notices a filesystem event in the /etc/bind/ folder:
#!/bin/sh # Copyright: Savonix Corporation - http://www.savonix.com/ # Author: Albert Lash # License: MIT # Date: 20090602 lockfile="/var/lock/rebuild_bind.lock" if [ -f $lockfile ] ; then exit 0 else touch $lockfile sleep 20 #/etc/init.d/bind9 restart /usr/sbin/rndc reload rm $lockfile exit 0 fi;
The man page mentions a third form of using flock, like this:
( flock -s 200 # ... commands executed under lock ... ) 200>/var/lock/mylockfile
That’s the one I’ll use. Actually, it took me a few minutes to figure this one… the lock wasn’t working for me when I was trying a non-blocking, exclusive lock. I did some more search and found this helpful page:
This is now what I’m using:
#!/bin/sh # Copyright: Savonix Corporation - http://www.savonix.com/ # Author: Albert Lash # License: MIT # Date: 20090602 exec 8>/var/lock/rebuild_bind.lock; if flock -n -x 8; then /usr/sbin/rndc reload sleep 30; fi
The reason why I’m using sleep? My incron setup occasionally results in many triggers getting fired, and I only want one to result in the rebuilding of the bind database. I’ll add some more logic to this, so that if something goes awry, it will alert me.