Finding process IDs with fuser

By Sandra Henry-Stocker  1 comment

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.

1 comment

    Anonymous 2 years ago
    Another tool for this purpose is lsof. I have found it to be very useful and usable.

      Add a comment

      Post a comment using one of these accounts
      Or join now
      At least 6 characters

      Note: Comment will appear soon after you have activated your account.
      Obscene/spam comments will be removed and accounts suspended.
      The information you submit is subject to our Privacy Policy and Terms of Service.

      ITworld LIVE

      IT Management/StrategyWhite Papers & Webcasts

      White Paper

      Evaluator Group: Storage Federation - IT Without Limits (Analysis of HP Peer Motion with Storage Federation)

      As the role of IT increases within organizations, the need to move data when and where it is needed is critical to support emerging business requirements. This has become increasingly difficult due to the huge growth of data volumes. This white paper sponsored by HP + Intel evaluates a solution that aims to enable the movement of data without physical limitations. Read now and see how this could enable agility and efficiency.

      White Paper

      ESG Lab Validation Report: HP Data Protector & Deduplication Solutions

      Many organizations have deployed disk-to-disk backup technologies to improve the speed and reliability of their backup and disaster recovery operations. A growing number of these now look to data deduplication to enhance retention periods and reduce costs. This ESG Lab Validation Report sponsored by HP + Intel examines a number of backup and recovery solutions and evaluates their ease of implementation as well as their ability to improve reliability and reduce costs.

      White Paper

      Business Value of Blade

      The nature of the blade platform makes system management, monitoring and provisioning easy and efficient. Access this resource to learn how blade migration will save your data center time and money while increasing performance.

      White Paper

      Accelerate time to application value

      For your IT organization to keep pace with the business, you need a new, faster approach to infrastructure deployment-an approach that increases agility and accelerates time to application value. That's HP Converged Systems. Built on Converged Infrastructure, these systems deliver the industry's first portfolio of pre-integrated, tested, and optimized infrastructure solutions for applications running in virtual, cloud, dedicated, or hybrid environments.

      White Paper

      Converged Infrastructure for Dummies

      As you know, everything is mobile, connected, interactive, and immediate. This is exactly why organizations need a highly agile IT infrastructure in order to keep pace with extreme fluctuations in business demand. This book will help you understand why infrastructure convergence has been widely accepted as the optimal approach for simplifying and accelerating your IT to deliver services at the speed of business while also shifting significantly more IT resources from operations to innovation.

      See more White Papers | Webcasts

      Ask a question

      Ask a Question