Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

See what process is using a file in Mac OS X

Note: This quesiton is NOT show me which files are in use. The file is not currently in use. The file will be in use at some unknown point in the future. At that point, I want to know what process accessed the file.

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.

like image 640
JPC Avatar asked Nov 29 '11 20:11

JPC


People also ask

How do I find the PID of a process on a Mac?

Run the command lsof -i : (make sure to insert your port number) to find out what is running on this port. Copy the Process ID (PID) from the Terminal output.

How do I see what processes are running on Mac Terminal?

Launch Terminal (Finder > Applications > Utilities). When Terminal is running, type top and hit Return. This will pull up a list of all your currently running processes. As in the Activity Monitor, this list shows your processes in decreasing order of how much of your resources they're consuming.

How do I stop a process in Mac OS X?

Click the Stop button in the upper-left corner of the Activity Monitor window (or use the Touch Bar). Choose one of the following options: Quit: This is the same as choosing File > Quit within an app.

What is lsof Mac?

lsof is a command that can be used to list all the open files. You can use grep to narrow down the listing to only those that match a certain string.


2 Answers

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

like image 145
spaceboy.cz Avatar answered Oct 16 '22 07:10

spaceboy.cz


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.

like image 97
Shaun Avatar answered Oct 16 '22 09:10

Shaun