November 14, 2007

Solving resource contention problems with fuser

Author: Duane Odom

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.

Category:

  • Tools & Utilities