38

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?

3 Answers 3

57

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?

4
  • Just some shared folders, but I know that all the clients on the network had ejected those shares and the entire server.
    – jamone
    Jul 12, 2010 at 11:59
  • 1
    Doesn't always matter. Sometimes the AFP/SMB/NFS/FTP/etc processes will leave open files just because the service is running. Best bet is to try to stop the service, then eject. That would be better than using the above command along with kill -9. You can stop a service via command line by something like sudo serveradmin stop smb, then start it by sudo serveradmin start smb, where "smb" is the name of the service. sudo serveradmin list will show them all.
    – churnd
    Jul 12, 2010 at 15:37
  • 1
    Normally it's Spotlight indexing stuff!
    – JGFMK
    May 3, 2019 at 23:36
  • For me, the culprit seems to consistently be Photos.app's photoanalysisd. You can use this command to kill photoanalysisd to enable ejection of the drive: launchctl kill -TERM gui/$UID/com.apple.photoanalysisd
    – rinogo
    Sep 26, 2021 at 20:46
8

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.

0

Use this cmdline to find the processes access to your drive:

sudo lsof | grep -v -e"^COMMAND" | grep -i YOUR_DRIVE_NAME | sort -u -k 1,2 | perl -n -e's/^\w+\s+(\d+).*/ps -p $1/; print $_'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .