
flock Command in Linux
The Linux flock command manages the file locks from script or commands to ensure the file is accessible to only one process at a time. It helps to prevent data corruption.
If multiple processes access or modify a file simultaneously, the file can be corrupted. The flock command locks the file so that it can only be accessed by one process at a time.
Table of Contents
Here is a comprehensive guide to the options available with the flock command in linux −
Syntax of flock Command
The syntax of the Linux flock command is as follows −
flock [options] [file | directory] [command] [arguments]
The [options] field is used to specify the options to control the commands behavior. The [file | directory] field is used to specify the file or the directory to be locked. The [command] is a command that the flock will run after successfully acquiring the lock. The [arguments] field is used to specify the [command] arguments.
flock Command Options
The options used with the flock command are listed below −
Options | Description |
---|---|
-c (--command) command | It is used to pass a single command without additional arguments |
-E (--conflict-exit-code) number | It is used to set the exit status code when there is a conflicting lock |
-F (--no-fork) | It is used to prevent the fork of a new process before executing the command |
-e , -x (--exclusive) | It is used to obtain the exclusive lock (write lock) |
-n (--nb , --nonblock) | It fails the command immediately if the lock is not obtained |
-o (--close) | It closes the file descriptor on which the lock is held before executing the command |
-s (--shared) | It is used to obtain the shared lock (read lock) |
-u (--unlock) | It is used to drop the lock |
-w (--wait , --timeout) seconds | It is used to set the timeout for a lock |
--fcntl | It is used to enable fnctl instead of flock |
--verbose | It reports how long it took to acquire a lock |
-h (--help) | It displays the brief help of command |
-V (--version) | It displays the command version |
Examples of flock Command in Linux
This section demonstrates the usage of the flock command in Linux with examples −
- Locking a File
- Stopping Command Execution if the File is Locked
- Using the Shared Lock
- Releasing the Lock
- Setting Timeout
Locking a File
To lock the file for one process only, use the flock command with the filename. For example, to lock a file named test.txt and keep it locked for 5 seconds using the sleep command, use the flock command in the following way −
flock test.txt sleep 5

If another process tries to access the file, it will only be able to do so after the first process releases the lock. After the execution of the sleep command, the file will be unlocked.
Stopping Command Execution if the File is Locked
If the file is locked then to immediately terminate the second access process, use the -n, --nb, or --nonblock option. These options stop the flock execution immediately if the lock is not obtained.
flock -n test.txt echo "Hello Linux"

In the output image, it can be seen that the second process that trying to access the test.txt file is immediately terminated.
Using the Shared Lock
The share lock option allows the file to be accessed by another process as well. To enable it, use -s or --shared options.
flock -s test.txt echo "Hello Linux"

The output shows the locked file is immediately accessed and modified using the shared option.
Releasing the Lock
If the file is being accessed by another process and locked, then to immediately release the lock and perform an operation, use the -u or --unlock option.
flock -u test.txt echo "Hello Linux"

Setting Timeout
If the file is locked, it is a good practice to wait for the file to be unlocked. For this purpose, the -w, --wait, or --timeout option is used.
flock -w 5 test.txt echo "Hello Linux"

The above command will wait for the file to be unlocked for 5 seconds. If the file remains locked then the command will be terminated.
Conclusion
The flock command in Linux is used to lock the file or a directory to make it inaccessible for more than one process. It is specifically used in scripts to avoid data corruption.
In this tutorial, we explained the flock command, its syntax, options, and usage in Linux through examples.