The flock command in Linux manages file locks from the script or the command line ensuring that only one process can access a file at a time to prevent data corruption caused by multiple processes accessing a single file simultaneously.
In simple terms, flock (file lock) locks the file so only one process can access the file and restricts other processes from utilizing that file to prevent data corruption which usually happens when multiple processes try to utilize a single file.
In this tutorial, I will walk you through how to use the flock command with multiple examples.
How to use the flock command in Linux
To get started with the flock command, it is recommended to learn the command syntax first. So here's a basic syntax that you need to follow to use the flock command:
flock [options] file [command]
[options]: it is used to modify the default behavior of the flock command.
file: it is where you specify the target file for the flock command.
[command]: an optional parameter which is where you specify the command that needs to be executed once the flock acquires the lock.
Now, let's take a look at some practical examples of the flock command.
1. Wait till the lock is released (the default behavior)
So you want to lock the file and apply a command over it but what if the lock is already acquired by another process? Well, waiting till the lock is released is a good idea.
To achieve this, you don't need any additional flags, it is a default behavior of the flock to wait till the lock is released.
Here's how you do it:
flock /path/to/file --command "the_actual_command"
To demonstrate this, I used the flock command on the same file side-by-side so the first execution will acquire the lock first meanwhile the second will wait for the lock to be released.
I will be using a sleep command on one side and the echo command on the other:
As you can see, first, I executed the sleep command on the left window which means the lock was acquired for 3 seconds meanwhile the command on the right window was executed too.
So the echo command (executed on the right window) needed to wait for 3 seconds before execution.
2. Stop if the file is locked
While waiting for the file lock to be released is a great idea, sometimes we want to get things done quickly and if the file is locked, you may want to change your approach than waiting for the lock to be released.
For that purpose, you'd have to use the
-n flag as shown here:
flock -n /path/to/file --command "the_actual_command"
Here's how it will behave if the target file is locked:
In the above example, the left window has a command that locks a file for 3 seconds whereas the right window tries to access the same file when the specified file is locked.
As a result, it returns nothing and does not wait for the file lock to be released.
3. Use a shared lock
Also known as a read lock which lets multiple processes concurrently access a shared resource without interfering with each other.
It is helpful when you want to let multiple processes read specific files but want to restrict write access.
To use the shared lock, you can use the
-s flag as shown here:
flock -s /path/to/file <command_to_execute>
For reference, I will be using the
-s flag twice at the same moment and as both commands only try to read the file content, they both will be executed:
4. Unlock the file
While the lock will be released automatically, you may find yourself in a situation where you want to release the lock and get things done.
For that purpose, you have to use the
-u flag as shown:
flock -u /path/to/file --command "actual_command"
To demonstrate this, I will use the flock command over a file to lock it and then on the other hand will use the
-u flag to show how it was unlocked:
If I didn't use the
-u flag, it wouldn't let me use the file.
Don't believe me? Here's a live example:
Learn more about file locking
Using the flock command is just a part of file locking in Linux but being such a complex topic, we decided to write a detailed guide explaining what is file locking in Linux:
I hope you will find this guide helpful.