View previous topic :: View next topic |
Author |
Message |
rogue Tux's lil' helper
Joined: 15 May 2002 Posts: 99 Location: Falls Church, VA
|
Posted: Thu Jul 10, 2003 9:44 pm Post subject: Finding out what process owns a given file |
|
|
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
- copy script into a file (i.e. ~/bin/processfd)
- chmod +x ~/bin/processfd
- 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 |
|
|
Pythonhead Developer
Joined: 16 Dec 2002 Posts: 1801 Location: Redondo Beach, Republic of Calif.
|
Posted: Thu Jul 10, 2003 9:59 pm Post subject: |
|
|
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 |
|
|
grzewho l33t
Joined: 31 Dec 2002 Posts: 626 Location: /home/g
|
Posted: Thu Jul 10, 2003 9:59 pm Post subject: |
|
|
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 |
|
|
rogue Tux's lil' helper
Joined: 15 May 2002 Posts: 99 Location: Falls Church, VA
|
Posted: Thu Jul 10, 2003 11:04 pm Post subject: |
|
|
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 |
|
|
Chris Finch Tux's lil' helper
Joined: 10 Mar 2003 Posts: 106 Location: Darmstadt, Germany
|
Posted: Wed Jul 16, 2003 9:43 am Post subject: |
|
|
Isn't fuser the program that does this? |
|
Back to top |
|
|
|