在工作过程中,我们进程需要查找某个文件被哪些进程占用。先上脚本:
#!/bin/sh
#example
#Find which process /dev/dri/card0 is occupied by
#./find_file.sh /dev/dri/card0
if [ $# -ne 1 ]
then
echo "Incorrect number of parameters, please add a parameter."
echo "example:Find which process /dev/dri/card0 is occupied by"
echo "./find_file.sh /dev/dri/card0"
exit 1
fi
for process in /proc/*
do
if [ ! -e ${process}/fd ]
then
continue
fi
str=`ls -l ${process}/fd | grep "$1"`
if [ -n "$str" ]
then
echo $process >> tt.log
echo $process
fi
done
脚本原理:
每个进程,在/proc目录下面都对应有一个子目录,每个子目录里面都有一个fd目录,每个fd目录里面记录了本进程都打开了哪些文件。