Linux show locked files

How to Lock a Text File in Linux Using flock Command

Before we explore the techniques/approaches of locking a text file under a Linux operating system environment, we should first understand the logic behind Linux’s file locking mechanism.

Linux’s file locking mechanism restricts/controls file access among multiple processes. When a text file is successfully locked, only one process can access it on a specific time schedule.

Before proceeding with this article, please understand that file locking is very much different from file encryption or file access control where a passphrase or password is needed to control user access to your files.

When a file is locked under a Linux operating system environment, a mutual exclusion event is created where only a single process can access it at a time.

Problem Statement

The Linux operating system will automatically block any write action attempts directed to an open file being written on by another process. However, what if you want to revoke the read and/or write permissions already invoked by the first process that has your file open and in write mode? Is there a workaround to this problem?

Moreover, we might also want to lock our file so that no other process interferes or attempt to disrupt the write mode status already initiated. This article will walk us through a viable solution to lock a text file in Linux.

Sample Reference File

For this tutorial to be more engaging and worthwhile, we should have some reference text files. We will concentrate on the text files inside the following directory:

Sample Text Files

For instance, let us open the file output.txt and start writing on it.

Write Content to File

While this file is still open, let us try to open it from another command line tab.

You will get the following response:

File is being edited by root

The above screen capture relays the PID (133110) of the process of working on the text file with the option of opening the file while it is still in that process’s write mode.

By keying in Y from our keyboard, we will have opened this text file and handed it over to a different process with exclusive write mode access.

Open Text Files

Therefore, a file modification by user 1 will lead to the following prompt on user 2 while attempting to save the file.

File was modified

This instance is a perfect representation of two different users on the network working on a single file.

Lock a Text File Using Linux’s flock Utility

To solve this issue, we need the aid of Linux’s flock utility. Since the util-linux package hosts flock command, it should be available in almost all Linux OS distributions. This command manages specific file/directory locks via the Linux command line environment.

To lock a text file in Linux, we will reference the following syntax:

$ flock -x PATH_TO_FILE_TO_LOCK -c COMMAND

The -x option is for obtaining a write lock to the targeted file. Let us attempt to lock the sample output.txt text file. The -c option will enable us to pass a single Linux supported command e.g. cat command.

$ flock -x /home/dnyce/LinuxShellTips_Files/output.txt -c cat

The terminal instance above will remain active to signify that the text file has been locked.

Читайте также:  Linux cmake include directory

If we open another terminal (while this terminal instance is still running) and execute another flock command on this same file, we should be denied access to it.

$ flock -w .007 /home/dnyce/LinuxShellTips_Files/output.txt -c echo; /bin/echo $? 1 

The -w option is used to relay the wait time of .007 seconds before a lock is placed on the text file. We then execute echo $? to output the exit status of this command.

An exit status of 0 implies that the command was executed successfully and an exit status of 1 implies the command could not be executed due to an error. In this case, the text file is under lock by another process.

To further confirm that the text file is under lock, we can use the lslocks commands to list all active local system locks.

List File Locks in Linux

As you can see, our file is present.

Unlocking a Text File in Linux

Canceling the initial flock command (Ctrl+c) or closing the text file should release the lock making it possible to successfully run the following command on the secondary terminal.

$ flock -w .007 /home/dnyce/LinuxShellTips_Files/output.txt -c echo; /bin/echo $?

Unlocking File in Linux

The exit status of 0 implies that the lock is no longer applicable to the text file.

We have learned how to lock a text file in Linux so that only one process can use it (write mode) at a time. The implementation of this article is particularly useful when different users on a network are accessing a single file.

More on flock command can be found on its man page.

Источник

How can I determine what process has a file open in Linux?

I’d like to determine what process has ownership of a lock-file. The lock-files are simply a file with a specific name that has been created. So, how can I determine what process has a particular file open in Linux? Preferably a one-liner type or a particular Linux tool solution would be optimal.

5 Answers 5

You can also use fuser for this:

~> less .vimrc # put in background ~> fuser .vimrc .vimrc: 28135 ~> ps 28135 PID TTY STAT TIME COMMAND 28135 pts/36 T 0:00 less .vimrc 

fuser has strange behavior with exit codes. it returns 1 exitcode with two states: A/ some internal error, checked file not found etc, B/ no process opened specified file. In situation A/ some error message is printed to its output. Unfortunately when file is available and opened by something, output is generated but with exit code 0. It would be better if fuser will exit with three codes, not two like currently. lsoft is a bit worse resolve because this is working more slowly.

This is essentially the same pattern that ls follows — it returns exit code 2 if there’s an error (e.g., invalid option specified) or file not found (and 0 if it successfully reports information).

On most Linux systems lsof NAME does the job:

fin@r2d2:~$ lsof /home/fin COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME bash 21310 fin cwd DIR 8,1 4096 5054467 /home/fin lsof 21320 fin cwd DIR 8,1 4096 5054467 /home/fin lsof 21321 fin cwd DIR 8,1 4096 5054467 /home/fin fin@r2d2:~$ 

@JoseLSegura: I assuming you’re resourceful enough for the answer ‘then install lsof’ to be useless for you. Can you elaborate on your problem? If you don’t have root, you likely don’t have privs to find out if another user has the file open anyhow.

Читайте также:  Linux изменить права файл терминал

@Jason: it does work for files, but cwd lines (which report use as the current working directory of a process) only report directories.

Having a file open is not a lock because, if each process has to check whether the file is open first and not proceed if it is or create/open it if it isn’t, then two processes could quite well check simultaneously, both find that it isn’t open, then both create or open it.

To use a file as a lock, the check-and-lock operation has to be a single uninterruptable operation. You can achieve this in a Unix filesystem by creating a file with read-only mode and removing it to unlock. If the file exists (and is read only) the file creation will fail, so you get check-and-lock in a single atomic operation.

If your locking process is a shell script that will be running as a daemon, you can get this effect by using umask , a per-process setting that sets the permissions that new files are created with:

oldumask=$(umask) umask 222 # create files unwritable to owner too if echo $$ > /var/lock/foo then : locking succeeded else : locking failed fi umask $oldumask

This also writes the owning process’ PID into the file, which solves your other problem: cat /var/lock/foo As regards the specific question «Which processes have this file open?», this can be useful when you want to unmount a filesystem but can’t because some process has a file open in it. If you don’t have those commands available, you can ask /proc as root:

ls -l /proc/*/cwd | grep ‘/var/lock/foo$’

ls -l /proc/*/cwd 2>/dev/null | grep ‘/var/lock/foo$’

Источник

File Locking in Linux

File locking in Linux is the solution by which you can ensure that the file for reading/writing is going to be handled safely.

Nope, this is not about password-protecting your files.

When multiple processes are working on the same file, you might not get intentional output as there was no order defined on how processes are going to handle the same file!

And file locking is the solution by which you can ensure that the file for reading/writing is going to be handled the safe way by defining the order.

It might sound too complex and that’s perfectly fine. So before approaching the solution, let me bring light to the problem.

Why File Locking is Needed

The problem is similar to the Race condition where multiple processes are modifying the same data simultaneously and the result strictly depends on the order of the process.

Let’s say there are two processes A and B which are dealing with the file named account.txt (initial data is 100)and their behavior is like this:

  • A reads the current value and subtracts 20 from the initial balance
  • B reads the current value and adds 50 to the balance.

On paper, everything looks excellent and the final data should be 130 but it ends up being something else. Let me show you how:

  • Process A: Reads data (100) and prepares for the operation.
  • Process B: Reads data (100) and saves the current data to its buffer.
  • Process A: Subsctracts 20 from 100 making it 80.
  • Process B: It is unaware of the fact that the original data has been modified and uses 100 as the initial value so the final result ends up being 150 (100 + 50) rather than what you expected (130).
Читайте также:  Linux проверка процессора команды

And there comes the implementation for file locking. So let’s have a look at the types of file locking in Linux.

Types of File Locking in Linux

There are two types of file locking in Linux:

1. Advisory Locking

The advisory locking system will not force the forces and will only work if both processes are participating in locking.

For example, process A acquires the lock and starts given tasks with a file.

And if process B starts without acquiring a lock, it can interrupt the ongoing task with process A.

So the condition for making an advisory lock is to ensure each process participates in locking.

2. Mandatory Locking

Mandatory locking can be understood as a forced locking system and it does not require any cooperation by the processes.

A mandatory locking causes the kernel to monitor every open, read, and write to verify that the process is not going to violate any process.

But making mandatory locking work requires comparatively more steps:

  • Mounting files with the “-o mand” option.
  • Also, users are required to turn off the group execute bit and turn on set-group-ID to enable mandatory locking as when you turn off the group-execute bit, set-group-ID will have no impact.

How to Enable Advisory Locking

For this, I’ll be using the flock command which allows users to handle tasks related to advisory locking. And the syntax for the flock command is as follows:

flock file_to_lock command

First, let’s make a file to hold the default value:

Now, let’s make it hold 100 as the default value:

So let’s create a simple script to handle the logical part of the processes:

#!/bin/bash file="account.txt" value=$(cat $file) echo "Read current balance:$value" #sleep 10 seconds to simulate business calculation progress=10 while [[ $progress -lt 101 ]]; do echo -n -e "\033[77DCalculating new balance..$progress%" sleep 1 progress=$((10+progress)) done echo "" value=$((value+$1)) echo "Write new balance ($value) back to $file." echo $value > "$file" echo "Done."

Now, let’s create the process a.sh with advisory locking:

And make sure it contains the following:

#!/bin/bash flock --verbose balance.dat ./update_balance.sh '-20'

Now, let’s make it executable:

So let’s put it to the test:

Behavior with Non-cooperative process

For this example, I’ll be creating the script b.sh which will be non-cooperative to its nature.

And script should contain the following:

#---------------------------------------- # non-cooperative way #---------------------------------------- ./update_balance.sh '80'

Make it executable by following the given command:

So let’s put it to the test:

As you can clearly see, my desired value was supposed to be 160 but the process b messed up and gave 180.

Behavior with Cooperative Processes

In this section, I’ll be making process b a cooperative process with some minor changes as given:

#---------------------------------------- # A cooperative way #---------------------------------------- flock --verbose balance.dat ./update_balance.sh '80'

As you can clearly see, process B waited while the first process was being executed and I got the desired results!

How to Inspect Locks in Linux

You can inspect all locks using lslocks command. And the usage is quite easy. A single command and that’s it:

list every lock in linux

And it will show everything from the command, and type of lock to the path.

Final Words

This guide explained all the basics related to locks in Linux with examples of how you can use advisory Linux for suitable tasks.

And if you have any queries, let me know in the comments.

Источник

Оцените статью
Adblock
detector