14

Bare with me as I am new to the flock tool and I haven't used file descriptors as far as I know.

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!

8

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
)
  • I do need the curl command would write to the file though. Am i misunderstanding how flock works? I'm new to the file descriptors :/ – DuckPuncher Feb 11 '15 at 17:29
  • 1
    @DuckPuncher The lock mechanism is separate from the read/write operations you can do on the file. So, curl can still write to the file. Note that this may be unsafe (or may not even work) with NFS, but do not use a shell script for file locking over NFS. With bash, you can also open the file for both reading and writing: 200<> resp.txt but this shouldn't change anything. – vinc17 Feb 11 '15 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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