- How to Find Out Who is Using a File in Linux
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- How find out which process is using a file in Linux?
- 4 Answers 4
- Find Which Linux Process is Using Your Files or Ports
- Use Fuser to Find Which Processes are Using a Filesystem
- Using LSOF (List Open Files)
- Find and Killing Processes Using Fuser
- Use Fuser to Find Which Processes / Daemons are Listening on a Port
- Kill Whatever Process is Listening on a Port
- Specifying the Signal Fuser Will Use as it Kills a Process
- Other Resources
- Files being used by a unix process
- Update
- 6 Answers 6
- How do I find out which processes are preventing unmounting of a device?
- 7 Answers 7
How to Find Out Who is Using a 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. In this case, an open file may be a regular file, a directory, a block special file, a character special file, a stream, a network file and many others – because in Linux everything is a file.
Lsof is used on a file system to identify who is using any files on that file system. You can run lsof command on Linux filesystem and the output identifies the owner and process information for processes using the file as shown in the following output.
To list user specific opened files, run the following command replace tecmint with the actual user name.
Another important use of lsof is to find out the process listening on a specific port. For example identify the process listening on port 80 using the following command.
Note: Since lsof reads kernel memory in its search for open files, rapid changes in kernel memory may result into unpredictable outputs. This is one of the major downsides of using lsof command.
For more information, look at the lsof man page:
That’s all! In this article, we have explained how to know who is using a particular file in Linux. We have shown how to identify the owner and process information for processes using an open file. Use the feedback form below to reach us for any questions or comments.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
How find out which process is using a file in Linux?
I tried to remove a file in Linux using rm -rf file_name , but got the error:
How can I find out which process is using this file?
4 Answers 4
You can use the fuser command, like:
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.
@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:
Note that you can also use lsof to find out what processes are using particular sockets. An excellent tool to have in your arsenal.
For users without fuser :
Although we can use lsof, there is another way i.e., we can query the /proc filesystem itself which lists all open files by all process.
Sample output below:
l-wx——. 1 root root 64 Aug 15 02:56 /proc/5026/fd/4 -> /var/log/filename.log
From the output, one can use the process id in utility like ps to find program name
Источник
Find Which Linux Process is Using Your Files or Ports
Find Which Linux Process is Using Your Files or Ports
If you have ever tried to unmount a filesystem, USB stick, cdrom, etc… but found that it was busy and therefore could not be unmounted, you will like the fuser command. Fuser’s man page tells us that fuser will “Show which processes use the named files, sockets, or filesystems.” But you can do more than that! You can also use fuser to send signals to your processes telling them to shutdown, terminate, pause, etc…
Use Fuser to Find Which Processes are Using a Filesystem
One of the things that used to annoy me the most was when I would try to unmount a filesystem and I would find that a process was using it and I would get the “device is busy” message.
Now, when I get that message, I simply use fuser to find which processes are using files in that filesystem.
Example: Show linux processes using /myfilesystem
As we look at the output from the fuser command above, we see that both the root user and the mary user have processes in the filesystem. We can review the columns and get lots of information. The USER, PID, and COMMAND columns are similar to the columns from the ps command and are self-explanatory. The USER column tells us the user id for the process. The PID column tells us the process id of that process. The COMMAND column tells us the command the process is running as well as the userid again in parenthesis. The ACCESS column tells us how the process is using the file. The ACCESS column may include he following letters referencing these access types:
ACCESS Column Code | Code’s Definition |
---|---|
F | file is open for writing. |
f,o | The process has an open file. |
r | The process’ root or home directory is on this filesystem (chroot). |
c | The process’ current directory is on this filesystem. |
e,t | The process is executing a file. |
m,s | The process has a mapped file or is using a shared library. |
Using LSOF (List Open Files)
We can also use the “lsof” (list open files) command to show a similar report.
Example: Using lsof to find processes using a specified filesystem.
Find and Killing Processes Using Fuser
For the more confident system administrators, fuser has a kill option (-k) that can be used to kill whatever processes are found using a file or filesystem.
Example: Find whatever processes are using the /myfilesystem and shut them down!
Interestingly, in the above example, we found that our very process was using the /myfilesystem filesystem. We ran the fuser command with the kill option (-k) telling it to kill the processes. As ours was amongst them, it killed our process.
Use Fuser to Find Which Processes / Daemons are Listening on a Port
Fuser has additional uses. Sometimes, we want to find out what process and userid are utilizing a port on our system. We can specify the IP version with -v4 for IPv4 or -v6 for IPv6.
Example: Find out what process is handling our http traffic on port 80
We were able to find out that varnishd is handling our http traffic on port 80/tcp.
Kill Whatever Process is Listening on a Port
Let’s imagine that you need to shutdown whatever is running on port 8001/tcp for some reason. Perhaps, you have a process that normally runs on that port, but something else has taken it while you have been performing some maintenance. We can use fuser to find and kill the process in one command by using the fuser command with the -k option.
Example: Find and kill whatever process is listening on port 8001/tcp
Specifying the Signal Fuser Will Use as it Kills a Process
You can change the signal used when killing a process using fuser by simply listing it as an option to your fuser command. You can list the available signals using “fuser -l”.
and then use withever signal you want it to use:
Other Resources
Fuser is a powerful tool that can help you find which linux process is using your files or ports. Here are a few other resources that can give you additional information on fuser.
Источник
Files being used by a unix process
The fuser command lets me know which processes are using a file or directory.
I’m looking for command that does the opposite: lets me know which files are being used by a process.
Update
Forgot to mention that it’s for a Solaris system.
6 Answers 6
lsof stands for “LiSt Open Files”. This shell command seems deceptively simple: It lists information about files opened by processes on a UNIX box.
Despite its (apparent) modest mission statement, lsof is actually one of the most powerful and useful UNIX commands. Its raw power comes from one of UNIX’s design principle often described as ”in UNIX everything is a file”. What this means is that the lsof concept of an open file not only covers regular files but also the following:
- Directories
- Streams or network files (for example, Internet or UNIX domain sockets and NFS files)
- Native libraries (for example, .soor .dylibdynamic libraries linked to a process)
- Block and character special files (for example, disk volume, external hard drive, console, or mouse)
- Pipes
Wait, I Cannot Find lsof on My System!
lsof is such a popular tool that it has been ported to pretty much all UNIX dialects (Linux, Mac OS X, BSD, Solaris, and so on). If it is unavailable on your box, use your usual package management system to install it. You can find lsof packages for Solaris on Sun Freeware.
Источник
How do I find out which processes are preventing unmounting of a device?
Sometimes, I would like to unmount a usb device with umount /run/media/theDrive , but I get a drive is busy error.
How do I find out which processes or programs are accessing the device?
7 Answers 7
Use lsof | grep /media/whatever to find out what is using the mount.
Also, consider umount -l (lazy umount) to prevent new processes from using the drive while you clean up.
Most of the time, the best command to use is lsof (“list open files”).
where /media/usb0 is the mount point of the USB drive or other filesystem to unmount. +f — tells lsof to treat the subsequent argument as a mount point; it usually, but not always, manages on its own, so that lsof /media/usb0 also works. This finds open files (even unlinked ones), memory mapped files, current directories, and some more obscure uses. You’ll need to run the command as root to get information on other users’ processes (and I think there are unices where lsof has to be run as root).
There are uses that lsof will not find; these are uncommon on removable media. They include:
- mount points: you can’t unmount /foo if /foo/bar is a mount point.
- mount devices: you can’t unmount /foo if /foo/bar is a mounted block device or loop-mounted regular file, or if it is the source of a Linux bind mount.
- NFS export: lsof won’t detect that a tree is exported by a kernel NFS server.
Another command that can serve in a pinch is fuser, which only lists PIDs of processes with open files on the device:
Источник