RattleyCooper
RattleyCooper

Reputation: 5207

Not sure how flock works. Need some clarification

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. My understanding is that this creates a subshell that can write to the locked file. I'm just not sure if I need to add the file descriptor to the curl request like this:

curl 'someurl' -H 'someHeader' 200>resp.txt

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

Upvotes: 4

Views: 7366

Answers (1)

hek2mgl
hek2mgl

Reputation: 158100

flock supports a command argument (-c). You can use the following one liner in your case:

flock resp.txt -c "curl 'someurl' -H 'someHeader' > resp.txt"

The above command tries to obtain an exclusive (write) lock for resp.txt. It waits unless the lock could be obtained if another script has locked the file.

As soon as the lock can be obtained it executes the curl command and releases the lock again.


If you want to perform multiple commands while obtaining the lock, I suggest to use a lockfile different from the file you are writing to.

Like this:

( 
    flock -n 200 || exit 1
    # commands executed under lock 
    sleep 3
    cat <<< 'hello' > test.txt
) 200> /path/to/lockfile

The above creates a sub shell () and redirects the output of that sub shell to /path/to/lockfile using the file descriptor number 200. The sub shell will inherit the file descriptor 200 from the base shell.

Inside the sub shell flock tries to obtain a lock for file descriptor 200. after obtaining the lock the following commands get executed.

Upvotes: 17

Related Questions