Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Finding out what process owns a given file
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
rogue
Tux's lil' helper
Tux's lil' helper


Joined: 15 May 2002
Posts: 99
Location: Falls Church, VA

PostPosted: Thu Jul 10, 2003 9:44 pm    Post subject: Finding out what process owns a given file Reply with quote

Sometimes you want to find out what process owns a given file but have no idea where to look or you have a billion processes running and don't want to search each one to see what files it owns.

For example, my brother recently installed Gentoo on his laptop and was having problems with sound. It would often die and when he tried to play something it would say something to the effect of /dev/dsp is in use.

He had no idea what to do, and neither did I so I decided I would write a script to figure out what process owns a given file so that we could see what process owned /dev/dsp and kill it. I thought this might be useful to some other people since it's fairly generic so here it is:

Code:
#!/bin/bash
# Robert Battle (07/10/03)
 
# check to see we are searching for something
SEARCH=$1
if [ "$SEARCH" == "" ]; then
        echo "usage: $0 term";
        exit 0
fi
 
# search for file descriptor matching argument
function search {
        local dir=$1
        local PROC=$2
        find $dir/fd -lname "*$SEARCH*" 2>/dev/null | while read file;
        do
                if [ $HEADER -eq 1 ]; then
                        echo "PID       COMMAND"
                fi;
                CMD=`cat $dir/cmdline`
                echo "$PROC     $CMD"
                return 1
        done
        return $?
}
 
 
HEADER=1
set -- $HEADER
for dir in /proc/*
do
        HEADER=$1
        # get processes
        PROC=`echo "$dir" | egrep -o "/([0-9]+)"`
        if [ "$PROC" == "" ]; then
                continue
        fi
 
        # remove leading /
        LEN=`expr length $PROC`
        LEN=`expr $LEN - 1`
        PROC=`expr substr "$PROC" 2 $LEN`
 
        search $dir $PROC
        rv=$?
        if [ "$rv" -eq 1 ]; then
                if [ "$HEADER" -eq 1 ]; then
                        HEADER=0
                        set -- $HEADER
                fi
        fi
done


To run this you need to do the following


  1. copy script into a file (i.e. ~/bin/processfd)
  2. chmod +x ~/bin/processfd
  3. processfd search term


There may be some magical way of doing this that I don't know about, but this works for me.
_________________
(rob)
Back to top
View user's profile Send private message
Pythonhead
Developer
Developer


Joined: 16 Dec 2002
Posts: 1801
Location: Redondo Beach, Republic of Calif.

PostPosted: Thu Jul 10, 2003 9:59 pm    Post subject: Reply with quote

Have you seen sys-apps/lsof ? It is similar to your script.

Code:
lsof /dev/dsp

COMMAND  PID USER   FD   TYPE DEVICE SIZE NODE NAME
xmms    2026  rob   11w   CHR   14,3       382 /dev/sound/dsp
xmms    2027  rob   11w   CHR   14,3       382 /dev/sound/dsp
xmms    2028  rob   11w   CHR   14,3       382 /dev/sound/dsp
xmms    2029  rob   11w   CHR   14,3       382 /dev/sound/dsp
xmms    2030  rob   11w   CHR   14,3       382 /dev/sound/dsp
xmms    2031  rob   11w   CHR   14,3       382 /dev/sound/dsp


Last edited by Pythonhead on Thu Jul 10, 2003 10:01 pm; edited 1 time in total
Back to top
View user's profile Send private message
grzewho
l33t
l33t


Joined: 31 Dec 2002
Posts: 626
Location: /home/g

PostPosted: Thu Jul 10, 2003 9:59 pm    Post subject: Reply with quote

you may also read the tips`n tricks section in this week`s gwn about other ways to do this using procps
_________________
Code:
USE="freedom -software_patents" emerge --deep --update world
Back to top
View user's profile Send private message
rogue
Tux's lil' helper
Tux's lil' helper


Joined: 15 May 2002
Posts: 99
Location: Falls Church, VA

PostPosted: Thu Jul 10, 2003 11:04 pm    Post subject: Reply with quote

I figured that there would be an easier way to do this, but after searching google and these forums for a while I couldn't find anything so I rolled my own. Thanks for the tips guys.
_________________
(rob)
Back to top
View user's profile Send private message
Chris Finch
Tux's lil' helper
Tux's lil' helper


Joined: 10 Mar 2003
Posts: 106
Location: Darmstadt, Germany

PostPosted: Wed Jul 16, 2003 9:43 am    Post subject: Reply with quote

Isn't fuser the program that does this?
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum