查看哪个进程正在访问 MacOSX 中的文件

注意: 这个问题是 没有告诉我哪些文件正在使用。该文件当前不在使用中。该文件将在未来某个未知的时间点使用。那时,我想知道哪个进程访问了文件。

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

86685 次浏览

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

$ 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 will 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).

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.

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

The faster way is:

$ lsof -r [path_to_file]

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

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

The -r argument keeps the command alive and should log any new file touched by the process you want to track.

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