Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How find out which process is using a file in Linux?

Tags:

linux

ps

I tried to remove a file in Linux using rm -rf file_name, but got the error:

rm: file_name not removed.  Text file busy 

How can I find out which process is using this file?

like image 262
khris Avatar asked Jul 03 '14 13:07

khris


People also ask

How do you find which process is creating a file in Unix?

The lsof command (already mentioned in several answers) will tell you what process has a file open at the time you run it. lsof is available for just about every unix variant.

How do you check which process is updating a file in Linux?

you can use lsof . this command is for find out what processes currently have the file open. if process opening the file, writing to it, and then closing it you can use auditing.

How to find processes that operate on files in Linux?

There are a couple of commands which can help us to find processes that operate on files, so we’ll start there. These commands gather data from the Linux kernel because it is responsible for running the processes, file systems, among other things. Additionally, we’ll read the kernel tables directly to get the needed info. 2.1. The fuser Command

How to find out who is using a particular file in Linux?

In this article, we will explain how to find out who is using a particular file in Linux. This will help you know the system user or process that is using an open file. We can use the lsof command to know if someone is using a file, and if they are, who. It reads kernel memory in its search for open files and helps you list all open files.

How can I tell what process has a file open?

The lsof command (already mentioned in several answers) will tell you what process has a file open at the time you run it. lsof is available for just about every unix variant.

How do I find the PID of a process in Linux?

To discover PID s of all processes using files in a directory and below, we can recursively scan it with +D. Additionally, lsof is frequently used to find all files opened by a given (by PID) process: 2.3. Getting Information From the Kernel Directly Another way of detecting the process of a file in use is by accessing the kernel directly.


2 Answers

You can use the fuser command, like:

fuser file_name 

You will receive a list of processes using the file.

You can use different flags with it, in order to receive a more detailed output.

You can find more info in the fuser's Wikipedia article, or in the man pages.

like image 180
jimm-cl Avatar answered Oct 08 '22 04:10

jimm-cl


@jim's answer is correct -- fuser is what you want.

Additionally (or alternately), you can use lsof to get more information including the username, in case you need permission (without having to run an additional command) to kill the process. (THough of course, if killing the process is what you want, fuser can do that with its -k option. You can have fuser use other signals with the -s option -- check the man page for details.)

For example, with a tail -F /etc/passwd running in one window:

ghoti@pc:~$ lsof | grep passwd tail      12470    ghoti    3r      REG  251,0     2037 51515911 /etc/passwd 

Note that you can also use lsof to find out what processes are using particular sockets. An excellent tool to have in your arsenal.

like image 24
ghoti Avatar answered Oct 08 '22 05:10

ghoti