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.