Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to have a file that is used as a counter. User A will write and increment this number, while User B requests to read the file. Is it possible that User A can lock this file so no one can read or write to it until User A's write is finished?

I've looked into flock but can't seem to get it to work as I expect it.

flock -x -w 5 /dev/shm/counter.txt echo "4" >  /dev/shm/counter.txt && sleep 5

If there's a more appropriate way to get this atomic-like incrementing file that'd be great to hear too!

My goal is:

LOCK counter.txt; write to counter.txt;

while at the same time

Read counter.txt; realize it's locked so wait until that lock is finished.
share|improve this question
1  
"can't seem to get it to work as I expect it." What specifically does that mean? –  John1024 Dec 29 '13 at 19:54
    
I'm sure I'm using it incorrectly, but while using the code above I can use two shells and have them both edit the counter file while I thought one was locking it. So if I run that line twice on two separate shells, (each with its own number to echo), I still see the second number being written –  d-_-b Dec 29 '13 at 19:56
1  
When the first completes and releases its lock, the second runs. Isn't that the way it should work? (In the code above, && sleep 5 is executed after the flock releases the lock.) –  John1024 Dec 29 '13 at 20:00
    
Hmm intersting... So in that case it would have been too quick for my human eye to catch. Great! My next challenge is how to put multiple commands in flock, but I'll put that as a separate question. Thanks John! –  d-_-b Dec 29 '13 at 20:02
1  
@d-_-b I love your username. That is THE MOST clever username I have ever seen. –  Devyn Collier Johnson Dec 29 '13 at 20:08

2 Answers 2

up vote 3 down vote accepted

Bash's processing of the command below may be surprising:

flock -x -w 5 /dev/shm/counter.txt echo "4" >  /dev/shm/counter.txt && sleep 5

Bash first runs flock -x -w 5 /dev/shm/counter.txt echo "4" > /dev/shm/counter.txt and, if that completes successfully (releasing the lock), then it runs sleep 5. Thus, the lock is not held for the 5 seconds that one may expect.

share|improve this answer

File locks aren't mandatory1 -- i.e., you can't lock a file so that another process cannot access it. Locking a file means that if a(nother) process checks to see if it has been locked, it will know.

The purpose of flock is to do stuff like what you want, but you must then use flock for each and every attempted access. Keep in mind these are blocking calls:

from man flock

if the lock cannot be immediately acquired, flock waits until the lock is available


1. Which makes the feature seem useless if you are using it for, e.g., security, but that is not the purpose of file locks -- they're for synchronization, which is what you are doing.

share|improve this answer
    
Thanks! This helps make sense of the 'advisory' aspect and how I need to use flock each time. very helpful! –  d-_-b Dec 29 '13 at 20:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.