Find out which application is using external hard drive in order to eject it

Alec Jacobson

November 23, 2009

weblog/

When using the Mac OS X UI to eject an external hard drive, I occasionally get this error: 'The disk "[volume name]" is in use and could not be ejected. \ Try quitting applications and try again." Only mac won't tell me which applications are using this drive. I hopelessly try to kill applications by force quitting or even using activity monitor to zap processes. I think I have finally solved this. Open up the program called Terminal.app (located in Applications/Utilities). Enter the following replacing [name of drive] with the name of your hard drive:
sudo lsof +D "/Volumes/[name of drive]"
You should get a list that looks something like this:
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
bash      637  ajx  cwd    DIR   14,2      442 2993107 /Volumes/path/to/file/in/use
preview   638  ajx  cwd    DIR   14,2      442 2993107 /Volumes/path/to/file/in/use
...
These are the applications/processes that are using your hard drive. They must be killed (quit) before you eject it safely. Try to quit any applications you recognize the normal way. For each of the other processes (line items) you want to issue the kill command followed by that process's PID. You might get errors in which case for each PID try the following until you don't get an error. You can always run the lsof command again to see if the process really died.
kill [PID of process]
sudo kill [PID of process]
sudo kill -INT [PID of process]
sudo kill -KILL [PID of process]
Update: So it seems sometimes lsof will just hang... I don't know what to do in this case, hopefully I will update again with further solutions.