Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Discover which programs prevent disk image ejection UNIX
One of my least-favorite error messages is the one that says a volume can't be ejected because it is in use. The vague advice to "try quitting applications" often leads me on a wild goose chase -- somehow it always seems to be the last application I try that has a document on the volume open.

I finally realized that the Unix lsof command is exactly what one needs in this situation. I have known about lsof for a long time, but somehow never put two and two together. Now when the Finder tells me I can't eject the volume 'DODO,' I just run this in the Terminal:
lsof | grep DODO
The output may then look something like this:
bash       228 johndoe  cwd      DIR      14,4      4096          2 /Volumes/DODO
Word      1379 johndoe   24u     REG      14,4        81     197704 /Volumes/DODO/C/Archiver/test.txt
This shows me that Word has test.txt open, and that there is a shell whose working directory is on DODO. This makes clear exactly what I need to do to be able to eject the volume -- close the Word document and cd out of the directory on DODO.

[robg adds: This use of lsof is mentioned in the commends to a much older hint I wrote on ejecting a busy disk image using fstat. At some point in OS X's history, it seems fstat vanished (at least as an end-user program) -- I can't find it on either my 10.4 or 10.5 partition, except as a BSD system call in OS X 10.5 -- leaving the use of lsof as the preferred way of finding out what's keeping a disk image busy. Due to that change, I felt it worth running this hint as a standalone refresh of the original. If you have another method, please post it in the comments.]

[Note: This is a near-duplicate of this older hint -- thanks to hayne for pointing this out to me. However, given there's a good chain of comments here, I'm going to leave this hint up. Sorry for the duplication.]
    •    
  • Currently 2.44 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (9 votes cast)
 
[25,366 views]  

Discover which programs prevent disk image ejection | 19 comments | Create New Account
Click here to return to the 'Discover which programs prevent disk image ejection' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Discover which programs prevent disk image ejection
Authored by: Stormchild on Jun 16, '08 07:56:21AM

This also applies to files that cannot be emptied from the trash or moved to another volume.



[ Reply to This | # ]
Discover which programs prevent disk image ejection
Authored by: mmnw on Jun 16, '08 08:14:59AM

Very good point. I actually knew about lsof and already used it with the cannot-eject-disk problem. But I never thought of using it with Trash, got to remember that. Thanks.



[ Reply to This | # ]
Discover which programs prevent disk image ejection
Authored by: harleyb on Jun 16, '08 08:02:46AM
somehow it always seems to be the last application I try that has a document on the volume open
Well, of course it's the last application you try. Why would you keep quitting apps if you've already fixed the problem? =P

[ Reply to This | # ]
Discover which programs prevent disk image ejection
Authored by: mmnw on Jun 16, '08 08:24:15AM
Why use grep? lsof offers an option to specifically search a directory path, i.e.
lsof +D /Volumes/DODO
Note that the uppercase "D" is important, a lowercase "d" would only search the directory/volume itself and not include any subdirectories.

[ Reply to This | # ]
Discover which programs prevent disk image ejection
Authored by: PatrickS on Jun 16, '08 11:28:22AM
From lsof(1):
lsof may process this option slowly and require a large amount of dynamic memory to do it. This is because it must descend the entire directory tree, rooted at D, calling stat(2) for each file and directory, building a list of all the files it finds, and searching that list for a match with every open file. When directory D is large, these steps can take a long time, so use this option prudently.
So using grep may be the faster way after all.

/Pat

[ Reply to This | # ]

Discover which programs prevent disk image ejection
Authored by: improbability on Jun 16, '08 08:26:52AM

lsof will only show your processes.
sudo lsof will show all processes.
Also "lsof +fg" will show file-flags -- Those listings with EVO will *NOT* prevent unmounting.



[ Reply to This | # ]
Discover which programs prevent disk image ejection
Authored by: kyngchaos on Jun 16, '08 08:32:14AM

One problem I find often is that it is file sharing that keeps me from ejecting a disk. With no applications running (including background apps), and noone connected to the disk over the network, I can't eject a disk. Only turning off file sharing lets me eject the disk.

I forgot about lsof, and will try it next time to see what exactly is keeping me from ejecting.



[ Reply to This | # ]
Discover which programs prevent disk image ejection
Authored by: mmnw on Jun 16, '08 08:45:53AM
That could certainly be. I had this problem a while ago with an external disk shared via SMB and AFP. Both protocols tended to block ejecting the disk. The worst was with windows clients on the network, because there is no obvious way of telling windows to close an open connection.
Also you should note the comment above, about using sudo lsof, because it could be, that file sharing uses a different user account to access the files (i.e. guest account), depending on your configuration.

[ Reply to This | # ]
Discover which programs prevent disk image ejection
Authored by: lowbatteries on Jun 16, '08 11:30:03AM

I wish OS X had this built in. Like a little "details" link in the "can't eject disk" dialog, that lists "file ZZZ is open by process YYY" - and even give a nice app name for processes that belong to a regular application.

After reading through the comments, I've come upon this as the fastest way to find the files:

lsof +fg +D /Volumes/DODO | grep -v EVO

And if that doesn't list anything, prefix a "sudo " to list from all users. Brilliant tip!



[ Reply to This | # ]
Discover which programs prevent disk image ejection
Authored by: asmeurer on Jun 16, '08 12:31:17PM

I use the program Sloth to do this. It shows all the open files on your system, and it is a simple step to find the ones from a particular drive.



[ Reply to This | # ]
Discover which programs prevent disk image ejection
Authored by: hamarkus on Jun 16, '08 12:36:29PM
The way I understand it, Sloth (http://www.sveinbjorn.org/sloth) gives you a GUI for this.
(This would be helpful if the filtering in Sloth would actually work, currently I have to look through the list of open files for the one I want to eject/delete.

[ Reply to This | # ]
Discover which programs prevent disk image ejection
Authored by: el_mancebo on Jun 16, '08 03:35:06PM

Activity Monitor, has a big "i" next to the "stop sign" that shows you the open files of an app.



[ Reply to This | # ]
Discover which programs prevent disk image ejection
Authored by: lihtox on Jun 16, '08 09:36:44PM
Activity Monitor, has a big "i" next to the "stop sign" that shows you the open files of an app.

Not to be completely dismissive, but that really solves the reverse problem: if I can't throw out a file, I want to know which application has that particular file open, not which files a particular application has open. Also, Activity Monitor's open-file interface (in 10.4, anyway) is inelegant: I just tried looking at the files held open by a few applications, for example, and they all seem to have dozens or hundreds of open files, with no way to search through them or even sort them alphabetically.

[ Reply to This | # ]

Discover which programs prevent disk image ejection
Authored by: bdm on Jun 17, '08 03:50:38AM
The program fuser can also help with this type of thing.

[ Reply to This | # ]
Discover which programs prevent disk image ejection
Authored by: jelockwood on Jun 17, '08 04:37:00AM

QuickTime Player and Preview both (under Tiger at least) regularly cause this problem for me. Besides stopping ejecting disk images, they also prevent ejecting CDs, DVDs or emptying the trash (if you have 'closed' the file and moved it to the trash). Basically these programs fail to properly close files.

This is a 'known' bug by Apple, I have not yet tried upgrading to QuickTime 7.5 to see if it fixes this.



[ Reply to This | # ]
Discover which programs prevent disk image ejection
Authored by: el_mancebo on Jun 17, '08 05:31:10AM

Secure empy trash, force to close the files in the trash and deletes them.



[ Reply to This | # ]
Discover which programs prevent disk image ejection
Authored by: regulus on Jun 17, '08 10:37:51AM
Today I wrote an application to do this. It's called "What's Keeping Me?" and it's free. If you're interested you can download it here:
http://www.hamsoftengineering.com/purchase/purchase.html


[ Reply to This | # ]
Discover which programs prevent disk image ejection
Authored by: PopMcGee on Jun 19, '08 12:03:43PM

My problem is that the list shows *zero* entries, yet OS X still prevents me from ejecting the volume. Bummer :-(



[ Reply to This | # ]
Discover which programs prevent disk image ejection
Authored by: Gabs on Jul 26, '08 04:39:49AM

I'll second the Sloth recommendation. It needed two goes to get a full list but then it was good. it seems Trash/Finder was the culprit inthis case. Odd.

BTW I did try the original script which delivered zero results, the modified version by mmnw resulted in an error.

Sloth - suits me fine!



[ Reply to This | # ]