MacOS – The disk “Vol” couldn’t be ejected because “Terminal” is using it

errorexternal-diskmacosterminal

I have connected an External hard disk to Mac and just issued command df -h in terminal. When I tried to eject drive it shows the error. After closing terminal, I could eject it. But is that really needed ? I didn't open any files in external disk. df -h just shows disk usage of all drives. Then how come terminal uses the drive and prevent from ejecting?

The disk “Vol” couldn’t be ejected because “Terminal” is using it

Best Answer

If you cd- change directory to the mounted filesystem then you must cd out of the filesystem before you can eject it. As an example

cd; diskutil eject /Volumes/<volume name>

cd: changes the current working directory to your home folder

diskutil: ejects the filesystem

There is one case where the tab needs to be closed. Sometimes the initial working directory is on the volume. (This can happen if you open a new tab from an existing tab that is visiting the volume.) When this happens, two processes are initially using the volume: login, and the shell it launches. cd will change the working directory in the shell, but won't affect the login process. The only way to make login stop using the volume is to close the tab.

If you're not sure which tab(s) to close, you can use lsof:

sudo lsof -a -c login /Volumes/<volume name> 

This command will show the process IDs (and other info) for all login processes that have open some subdirectory of the volume. With the process ID, you can either:

  • (if Terminal is configured to show the TTY name in the title):
    1. get the controlling terminal with ps (ps -o tty -p <login pid(s),>)
    2. find the relevant tabs from the Window menu
    3. close them
  • more simply, shut down the relevant login by sending it/them a SIGHUP (kill -HUP <login pid(s)>)