Open In App

Shell Script for a flexible file locking mechanism

File-locking mechanisms can be implemented in Linux using shell scripts. Locking files is required and becomes necessary to make a mutual exclusion environment where multiple processes can read and write the same file without violating consistency issues. A typical race condition occurs when the output of particular commands is different based on the relative order of execution of commands. This can be solved by locking files when required.

Linux provides two types of locking mechanisms:



1. Shared Locks:

Also called a read lock. This lock can be applied to files being read by a process. This prevents other processes from acquiring a write lock on the same file thus the data can’t be modified as long as the process is reading it. Although a shared lock by some other process can still be acquired on the same file.

2. Exclusive Locks:

Also called the write lock. This lock can be used on a file being modified ie being written by any process. Acquiring this lock prevents other processes from getting both the shared and exclusive locks. Other processes must wait before getting a shared or exclusive lock till this process releases the lock.



Utility commands to modify, view and acquire the lock

The Linux shell provides multiple utility commands to modify, view and acquire locks on a particular file.

Command 1: lslocks

lslocks is a member of the util-linux package and is available to all Linux distributions. used to list all the locks currently held by different processes on different files. Running the command on a Linux shell gives the output as a list of all the locks on different files by all the processes.

Command:

lslocks

The above output fields show different info about each lock like

  1. `COMMAND` tells the process which is holding the lock
  2. `PID` gives the process ID of the particular process
  3. `TYPE` gives the method type of lock acquisition
  4. `MODE` specifies whether the lock is a READ or WRITE lock
  5. `START, END` specify the starting and ending part of the file locked
  6. `PATH` specifies the path of the file locked

Command 2: flock

flock command is provided by the util-linux package. used to manage locks on file from shell scripts or command line

Syntax:

flock filename

Command:

touch test.txt
flock --verbose test.txt -c 'lslocks'

Shell Script

Now a shell script can be written which uses the flock command to manage the locks on different files.

Script:

#!/bin/bash

echo "=============FILE LOCKING=============="
echo "Enter locking Mode (R/W): "
read mode
if [ "$mode" = "R" ]; then
mode="-s"
else
mode="-x"
fi

echo "Enter file path: "
read path

echo "Command to execute after locking: "
read command

runcommand="flock $mode $path -c '$command'"
eval "$runcommand"

Output:

Running the above shell script

Explanation:

  1. The above shell script accepts the mode of locking from the user in the variable $mode.
  2. For read, the ‘-s‘ argument is added to the command, and for write mode ‘-x‘ argument is added.
  3. The file path to be locked is accepted as a string in the variable $path.
  4. During the locking period, the command that has to be executed is then inputted in the variable $command.
  5. The whole command to lock the file and execute the user’s command is created from the above parameters in variable $runcommand.
  6. $runcommand is executed in the terminal.

Conclusion:

The above shell script can be used for flexible file locking according to the user’s needs. File locking can help during operations where mutual exclusion is required. Any file can be locked by providing the path to the file, locking mode, and the command to execute during the file locking, the script automatically generates the required command and performs the operation.

Article Tags :