How to use flock and file descriptors to lock a file and write to the locked file?

I have a bash script that makes a cURL request and writes the output to a file called resp.txt. I need to create an exclusive lock so that I can write to that file and not worry about multiple users running the script and editing the text file at the same time.

Here is the code that I expect to lock the file, perform the request, and write to the text file:

(
flock -e 200
curl 'someurl' -H 'someHeader' > resp.txt
) 200>/home/user/ITS/resp.txt

Is this the correct way to go about this? My actual script is a bit longer than this, but it seems to break when I add the flock syntax to the bash script.

If someone could explain how these file descriptors work and let me know if I am locking the file correctly that would be awesome!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This is not correct because when you do ( flock -e 200; ... ) 200> file, you are truncating the file file before you get the exclusive lock. I think that you should do:

touch resp.txt
(
  flock -e 200
  curl 'someurl' -H 'someHeader' > resp.txt
) 200< resp.txt

to place the lock on the file opened as read only.

Note. Some shells do not support file descriptors larger than 9. Moreover the hardcoded file descriptor may already be used. With advanced shells (bash, ksh93, zsh), the following can be done:

touch resp.txt
(
  unset foo
  exec {foo}< resp.txt
  flock -e $foo
  curl 'someurl' -H 'someHeader' > resp.txt
)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x