Identify the files opened a particular process on linux

微笑、不失礼 提交于 2019-12-03 06:02:41

问题


I need a script to identify the files opened a particular process on linux

To identify fd :

>cd /proc/<PID>/fd; ls |wc –l  

I expect to see a list of numbers which is the list of files descriptors' number using in the process. Please show me how to see all the files using in that process. Thanks.


回答1:


The command you probably want to use is lsof. This is a better idea than digging in /proc, since the command is a more clear and a more stable way to get system information.

lsof -p pid

However, if you're interested in /proc stuff, you may notice that files /proc/<pid>/fd/x is a symlink to the file it's associated with. You can read the symlink value with readlink command. For example, this shows the terminal stdin is bound to:

$ readlink /proc/self/fd/0
/dev/pts/43

or, to get all files for some process,

ls /proc/<pid>/fd/* | xargs -L 1 readlink



回答2:


While lsof is nice you can just do:

ls -l /proc/pidoftheproces/fd



回答3:


lsof -p <pid number here> | wc -l

if you don't have lsof, you can do roughly the same using just /proc

eg

$ pid=1825
$ ls -1 /proc/$pid/fd/*
$ awk '!/\[/&&$6{_[$6]++}END{for(i in _)print i}' /proc/$pid/maps



回答4:


You need lsof. To get the PID of the application which opened foo.txt:

lsof | grep foo.txt | awk -F\  '{print $2}'

or what Macmede said to do the opposite (list files opened by a process).




回答5:


lsof | grep processName


来源:https://stackoverflow.com/questions/2681501/identify-the-files-opened-a-particular-process-on-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!