There are times when finding out what processes are using
certain resources is invaluable to an administrator or a regular user. Invariably,
one will come across the extremely frustrating “umount: /media/cdrom: device is busy” or a similar error
message. Poking around, you may or may not be able to figure out why the device
is busy.

One simple little tool to help figure this out is the fuser tool. What fuser does is identify the
processes that are using certain files or sockets. With this in mind, you can
easily determine which processes have “locked” /media/cdrom (or the mount point in question) by executing:

$ fuser -m /media/cdrom
/media/cdrom:            21048c

This command tells fuser to report the processes that are
using resources on the specified mounted file system (via the -m option).

From the output above, fuser tells us that process ID 21048 is
using something on /media/cdrom. By
then using ps, we can determine which
process is, in fact, locking the device:

$ ps auxw|grep 21048|grep -v grep
joe   21048  0.0  0.1  17820  2632 pts/0    Ss   11:58   0:00 -zsh

Our ps call is piped
through grep to search for PID 21048;
we use grep again to negate it,
finding the grep process searching
for the PID. As can be seen here, the zsh
process, run by user joe, is locking
the file system. This probably means that joe
is logged in using the zsh shell, and
his current working directory is somewhere in /media/cdrom.

Knowing this, you can either kill joe‘s shell or ask him kindly to move out of the directory to allow
you to unlock umount /media/cdrom.

The fuser output that displays has an additional character,
however. In the above example, it showed 21048c. The c indicates that
the access type is “current directory,” which we had determined with ps. Other access types that fuser can
indicate are e for a running executable,
f for an open file, r for the root directory, and m for a shared library. To see these
different access types in action, type fuser
-m /usr
and you should see a number of processes with one (or more)
associated access types.

Fuser can display a lot of information; it can display even
more if passed the -a flag, which
shows all files specified. You also don’t need to specify a mount point. If you
want to see what process is using a particular file, you can do that too.

Delivered each Tuesday, TechRepublic’s free Linux NetNote provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!