bash and flock (file lock) - Doesn't seem to be locking....

Posted by Rory on Stack Overflow See other posts from Stack Overflow or by Rory
Published on 2010-05-24T11:34:13Z Indexed on 2010/05/24 11:51 UTC
Read the original article Hit count: 358

Filed under:
|
|
|
|

I am playing with using flock, a bash command for file locks to prevent 2 different instances of the code from running more than once.

I am using this testing code:

( ( flock -x 200 ;  sleep 10 ; echo "original finished" ; ) 200>./test.lock ) &
( sleep 2 ; ( flock -x -w 2 200 ; echo "a finished" ) 200>./test.lock ) &

I am running 2 subshells (backgrounded). The (flock NUM; ...) NUM>FILE syntax is from flock's man page.

I expect that the first subshell will get an exclusive lock on test.lock, then wait 10 seconds, then print "original finished", all the time holding the lock. The second subshell will start at more or less the same time, wait 2 seconds, then try to get a lock on test.lock, but timeout after 2 seconds. If it gets a lock, then it'll print "a finished". If it doesn't get the lock, that subshell should stop, and nothing should be printed.

Since the first subshell is waiting longer, it will keep the lock for 10 seconds, so the second subshell should not get the lock, and shouldn't finish. i.e. one should see "original finished" printed and not both.

What actually happens is that "a finished" is printed, then "original finished" is printed.

This implies that that the second subshell is either (a) not using the same lock as the first subhsell or (b) that it fails to get the lock, but continues to execute or (c) something else.

Why don't those locks work?

© Stack Overflow or respective owner

Related posts about linux

Related posts about bash