bash lsof 应该为一组 pid 提供所有打开的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12468629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:42:55  来源:igfitidea点击:

lsof should give all open files for a set of pids

bashshellscriptingfreebsdlsof

提问by user1071840

I have 30 instances of a process running on a server and want to log open files for each process for analysis.

我有 30 个在服务器上运行的进程实例,并希望记录每个进程的打开文件以进行分析。

I ran the following command:

我运行了以下命令:

* ps auwwx | grep PROG_NAME | awk '{print }' | xargs lsof -p | less

It complaints that, "lsof: status error on : No such file or directory"

它抱怨说,“lsof:状态错误:没有这样的文件或目录”

However, if I run lsof -p < pid >it gives me the list of open files for that process . How can I get a list of all open files for all 30 instances of the process on a FreeBSD machine.

但是,如果我运行lsof -p < pid >它,它会给我该进程的打开文件列表。如何获取 FreeBSD 机器上所有 30 个进程实例的所有打开文件的列表。

Moreover, I do not want the shared libraries to be listed. If I do -d "^txt"it isn't showing some other db files which I want to be shown. Is there any other way to grep out the .so files?

此外,我不希望列出共享库。如果我这样做,-d "^txt"它不会显示我想要显示的其他一些数据库文件。有没有其他方法可以 grep 出 .so 文件?

回答by Ben Hymanson

The lsof -poption takes a comma-separated list of PIDs. The way you're using xargswill pass the pids as separate arguments leading some to be interpreted as filenames.

lsof -p选项采用逗号分隔的 PID 列表。您使用的方式xargs将 pids 作为单独的参数传递,导致一些被解释为文件名。

Try lsof -p $(your grep | tr '\012' ,)That's going to have a trailing comma, I'm not sure if lsofwill care but you could sedit off if necessary.

尝试lsof -p $(your grep | tr '\012' ,)这将有一个尾随逗号,我不确定是否lsof会在意,但sed如果需要,您可以将其关闭。

回答by nneonneo

You can use xargs -L1 lsof -pto run lsofonce per pid.

您可以使用每个 pidxargs -L1 lsof -p运行lsof一次。

Even better: use lsof -cto list all open files from commands matching a specific pattern:

更好的是:用于lsof -c从匹配特定模式的命令中列出所有打开的文件:

lsof -c bas # list all processes with commands starting with 'bas'
lsof -c '/ash$/x' # list all commands ending with 'ash' (regexp syntax)