OS X determine which application is accessing a HDD and preventing ejection?

I'm running OS X 10.6 Server, and I want to eject my external drive so I can do some disk maintenance such as defraging it. However when I try to eject the drive it fails saying the disk is in use. I can force eject it but that could cause corruption... How can I tell which application is using the drive and holding it open?


Try sudo lsof | grep /Volumes/External, where "External" would be the name of your external drive. Are you hosting any services' data off of that drive?


lsof - List of open files

At your command promt just type sudo lsof to see a list of open files and their location and process id so that you can kill the process.

To refine it a little bit you could use sudo lsof | grep hard drive name.

Once you have the pid sudo kill -9 pid to kill the naughty process.