IT management/strategy

Finding process IDs with fuser

March 25, 2009, 11:38 AM — 

The fuser process is a handy command for getting information about processes that are using particular files. I discovered this command quite a few years ago and rely on it when I need to unmount a file system, find out that it's in use and then need to figure out why.

Fuser doesn't only come in handy when you need to unmount a file system, however. It's also useful command for many other situations in which you need to figure out who is using some particular file or command.

To get a quick look at how easily fuser can provide a list of process IDs associated with some particular resource, try running the commands included in this bash script:

#!/bin/bash

if [ $# == 0 ]; then
    echo -n "file> "
    read FILE
else
    FILE=$1
fi

PROCIDS=`fuser $FILE 2>/dev/null`
echo $PROCIDS

Stripping away standard error in this script removes the letter codes that indicate how each file is being used by each of the processes (see table below), but if the process ID is all you need for further processing, ignoring the letter codes can simplify the process of preparing a list of process IDs.

c     Indicates that the process is using the  file  as  its current directory.

m     Indicates that the process is using a file mapped with mmap(2). See  mmap(2) for details.

o     Indicates that the process is using  the  file  as  an open file.

r     Indicates that the process is using the  file  as  its root directory.

t     Indicates that the process is using the  file  as  its text file.

y     Indicates that the process is using the  file  as  its controlling terminal.

To generate a list of which processes are using bash right now, you could just type "showUsers bash":

# showUsers /bin/bash
7998 7995 2043 2042 2041 962 28488 28487 5159 5157

If you're looking for bash usage, of course, you're going to find the process ID associated with running the script in the list! You could add this line to the end of the script to make this clear:

echo I am $$

Your output might then look like this:

showUsers bash
8246 8243 2043 2042 2041 962 28488 28487 5159 5157
I am 8243

You could also just exclude your script's process ID in later processing if you're concerned that it will trip over itself in searching for other file users.

for PID in `echo $PROCIDS`
do
    if [ $PID != $$ ]; then
        echo $PID
	...
    fi
done

Using fuser instead is ps, grep and awk commands to find process users is far simpler and more accurate than "ps -ef | grep FILE | awk '{print $2}'" commands and works with files as well processes.

Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world

I like it!
Comments

Another useful tool

Another tool for this purpose is lsof. I have found it to be very useful and usable.
| reply
peer-to-peer

Esther Schindler
If the comments are ugly, the code is ugly

claird
SVG a graphics format for 21st century

pasmith
Take Chrome OS for a test spin

Sandra Henry-Stocker
Solaris Tip: Have Your Files Changed Since Installation?

sjvn
64-bits of protection?

jfruh
Android fragments vs. the iPhone monolith

mikelgan
What Gizmodo missed about the Pro WX Wireless USB disk drive

 

Where Google Chrome security fails: the password
I heard mention that the Chrome OS will have some sort of encryption available a la bitlocker. If it's possible to encrypt personal data using another password or key, then it may have potential for very secure data.... And Ubuntu has an 'encrypt home directory' option, perhaps google should follow suit.
- Dann

Join the conversation here

The Daily Tip

The Daily TipQuick, practical advice for IT pros. Made fresh daily.

Hot tips:

Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.

Newsletters

Subscribe to ITWORLD TODAY and receive the latest IT news and analysis.

I would like to receive offers via email from ITworld partners.
By clicking submit you agree to the terms and conditions outlined in ITworld's privacy policy.
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

Marketplace