[NTLUG:Discuss] Flock file hanging around

Steve Litt slitt at troubleshooters.com
Mon Jan 28 14:20:20 CST 2013


On Mon, 28 Jan 2013 12:46:34 -0600
"Neil Aggarwal" <neil at JAMMConsulting.com> wrote:

> Hello All:
> 
> I wrote a shell script using flock:
> 
> #!/bin/bash
> (
> flock -x 200
> # Perform tasks here...
> sleep 5
> ) 200>/var/lock/mylock
> 
> After the script exits, the mylock file is still there.
> I thought flock is supposed to remove it after the script finishes.
> 
> Any ideas?

That is soooooo cool! Thanks for telling me about flock. I didn't know
about it til you told me.

I did some experiments, and putting rm /var/lock/mylock after the
closing parenthesis isn't the answer, because by that time another
invocation of the script could be using the same lockfile.

Let me ask you a question: What's the harm if /var/lock/mylock remains
afterward? From my understanding, after the lock terminates, the file
is just another file. And if something else locks it, it re-assumes its
role as a lockfile.

This is soooooo cool! I can hardly wait to find a reason to use it.
Thanks so much.

SteveT



More information about the Discuss mailing list