macos 查看 Mac OS X 中哪个进程正在使用文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8317177/
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-10-21 08:39:02  来源:igfitidea点击:

See what process is using a file in Mac OS X

macosunixterminalauditlsof

提问by JPC

I would like to be able to track a file and see which process is touching that file. Is that possible? I know that I can see the list of open processes in activity monitor but I think it's happening to quickly for me to see it. The reason for this is I'm using a framework and I think the system version of the framework is being used instead of the debug version and I'd like to see which process is touching it.

我希望能够跟踪文件并查看哪个进程正在访问该文件。那可能吗?我知道我可以在活动监视器中看到打开的进程列表,但我认为我很快就能看到它。这样做的原因是我正在使用一个框架,我认为正在使用该框架的系统版本而不是调试版本,我想看看哪个进程正在接触它。

采纳答案by Shaun

lsofwill list open files, but it can be a bit awkward for momentary touches (eg, if the file isn't open when lsof runs, it doesn't show).

lsof将列出打开的文件,但对于暂时的触摸可能有点尴尬(例如,如果 lsof 运行时文件未打开,则不会显示)。

I think your best bet would be fernLightning's fseventer.app. It's "nagware", and allows you to watch (graphically) the fsevents API in real-time.

我认为你最好的选择是 fernLightning 的fseventer.app。它是“nagware”,允许您实时(以图形方式)观看 fsevents API。

回答by spaceboy.cz

That's simple: sudo fs_usage | grep [path_to_file]

这很简单: sudo fs_usage | grep [path_to_file]

回答by Michael Dautermann

But I spent 2 minutes Googling and found your answer here.

但我花了 2 分钟谷歌搜索并在这里找到了你的答案

$ lsof | grep [whatever]

Where [whatever] is replaced with the filename you're looking for. With this, you can see which program is desperately holding onto your about-to-be-trashed file. Once you exit that program, your trash will empty.

$ lsof | grep [随便]

其中 [whatever] 替换为您要查找的文件名。有了这个,您可以看到哪个程序拼命保留您即将被删除的文件。退出该程序后,您的垃圾箱将清空。

回答by bontoJR

The faster way is:

更快的方法是:

$ lsof [path_to_file]

This solution doesn't require the root password and gives you back the following, clear, result:

此解决方案不需要 root 密码,并为您提供以下清晰的结果:

COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME
Finder     497  JR7   21r   REG    1,2   246223 33241712 image.jpg
QuickLook 1007  JR7  txt    REG    1,2   246223 33241712 image.jpg

回答by cavalcade

Another option is Sloth. It's a free, open source GUI for LSOF that others have mentioned.

另一种选择是Sloth。这是其他人提到的用于 LSOF 的免费、开源 GUI。