Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
"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
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
@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
up vote 6 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 those 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. User Leo pointed out that there may be a non standardized implementation of mandatory file locking in the linux kernel (see this discussion), based on historical parallels from other *nix operating systems. However, this looks to be a C level interface only.

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


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.