This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature: Tools & Utilities

Solving resource contention problems with fuser

By Duane Odom on November 14, 2007 (4:00:00 PM)

Share    Print    Comments   

Have you ever tried to unmount your USB thumb drive only to get the message "device is busy"? Like me, you probably thought, "I know I closed that file manager window. What's keeping it busy now?" fuser is a command that can answer that question for you, along with similar questions about what processes are using what files or sockets.

The fuser command command takes as an argument either a file path, the name of a mounted filesystem, or a namespace/port combination (eg. Telnet/TCP). If the specified resource is being used, fuser displays the name of the resource along with the ID of each process accessing it, followed by a letter signifying the type of access the process has. The following is a list of possible access types:

  • c -- the process's current directory was specified
  • e -- the process's executable file was specified
  • f -- the process has the specified file open for reading
  • F -- the process has the specified file open for writing
  • r -- the process is rooted at the specified directory
  • m -- the process has the specified file memory mapped or loaded as a shared library

In the USB drive example above, if you type fuser -v -m /media/usbdrive, fuser reports back with something like:

                  USER    PID  ACCESS COMMAND
/media/usbdrive:   duane   4533 ..c.. bash

The -m tells fuser that you are specifying a mount point (or a file on that mount point) and the -v tells fuser that you want verbose output, which gives you the column headers and displays the owner of the process. Looking at the output you can see that the ID of the process that's accessing the USB thumbdrive is 4533. If you would like to know more information about that process, you can run the command ps aux | grep 4533. If you just want to kill the process so you can unmount the drive, you can add -k to fuser's command line and all processes that are accessing the specified mount point will be killed. If you are unsure about killing the process without reviewing it first, you can add -i with -k to prompt you before you kill each process.

fuser can also tell you which processes are accessing network ports. For example, if you try to start an FTP server and you get a message indicating that the port is busy, you can run the command fuser -v -n tcp 21 and fuser will display

          USER  PID  ACCESS COMMAND
21/tcp:    root  4391 F.... vsftpd

This means that the vsftpd FTP server is already running on port 21. Here again you can automatically kill the process by adding -k and prompt before killing by adding -i. If you are not familiar with the ports on which services run, you can specify the name of the service to fuser instead (eg. fuser -n tcp ftp).

fuser is a useful command-line tool that can give you the information you need to free up resources so you can safely unmount a USB drive or start your FTP server. fuser can also be used in scripts to determine whether a file is being used before deleting it, whether a port that your application wants is already being used by another service, or whether a mounted filesystem is being used before you unmount it to perform a filesystem check. As with every other command, you can learn more about fuser and its switches by looking at its man page.

Duane Odom is a computer programmer for the US Department of Defense and a freelance writer. He has been a Linux user since 2001.

Share    Print    Comments   

Comments

on Solving resource contention problems with fuser

Note: Comments are owned by the poster. We are not responsible for their content.

Solving resource contention problems with fuser

Posted by: Anonymous [ip: 193.144.12.226] on November 14, 2007 04:15 PM
You run mount and you get the usb device, something like: /dev/sdc1.



Then you run lsof /dev/sdc1


and you get more or less what fuser output to you.



If I have lsof why do I need fuser?


Thank you.


adrian15

#

Re: Solving resource contention problems with fuser

Posted by: Anonymous [ip: 198.97.67.59] on November 14, 2007 06:02 PM
Because some systems like Solaris 8 don't have lsof, but do have fuser. And besides, it doesn't appear as if lsof can kill the process in question with out additional commands and the like, and this might be useful to someone somewhere trying to automate a process.

Hobbletoe

#

Re: Solving resource contention problems with fuser

Posted by: Jeremy Akers on November 15, 2007 12:22 AM
fuser is just faster and simpler.



-Jeremy

#

Solving resource contention problems with fuser

Posted by: jose on November 15, 2007 06:40 PM
The manual page for lsof is so big as the Bible, and use this command is enough in Linux for this task.



--
http://killminus9.blogspot.com

#

Solving resource contention problems with fuser

Posted by: Anonymous [ip: 190.144.32.10] on November 16, 2007 11:11 PM
solaris have lsof....

check your manual....

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya