How do I find out which processes are preventing unmounting of a device?
Sometimes, I would like to unmount a usb device with umount /run/media/theDrive , but I get a drive is busy error. How do I find out which processes or programs are accessing the device?
8 Answers 8
Use lsof | grep /media/whatever to find out what is using the mount.
Also, consider umount -l (lazy umount) to prevent new processes from using the drive while you clean up.
fuser -mv /path/to/mountpoint might be a more readable alternative for finding out processes using a mointpoint.
@RiccardoMurri lsof | grep works better for me. fuser -mv seems to just dump 80+ of unrelated processes. I am using mount binded directories.
umount -l is dangerous. mount -o bind a mode 000 empty directory on top instead, and clean up via lsof +f — /dev/device .
Most of the time, the best command to use is lsof (“list open files”).
where /media/usb0 is the mount point of the USB drive or other filesystem to unmount. +f — tells lsof to treat the subsequent argument as a mount point; it usually, but not always, manages on its own, so that lsof /media/usb0 also works. This finds open files (even unlinked ones), memory mapped files, current directories, and some more obscure uses. You’ll need to run the command as root to get information on other users’ processes (and I think there are unices where lsof has to be run as root).
There are uses that lsof will not find; these are uncommon on removable media. They include:
- mount points: you can’t unmount /foo if /foo/bar is a mount point.
- mount devices: you can’t unmount /foo if /foo/bar is a mounted block device or loop-mounted regular file, or if it is the source of a Linux bind mount.
- NFS export: lsof won’t detect that a tree is exported by a kernel NFS server.
Another command that can serve in a pinch is fuser, which only lists PIDs of processes with open files on the device:
How do I find out what processes are accessing the hard disk in a GNU/Linux-based system?
I’m looking for the equivalent to top for disk access, so I can tell which process(es) are currently reading and/or writing to disk. I’m currently using Ubuntu, but I imagine there’s a standard tool that’s available as part of the GNU toolset.
4 Answers 4
You got three-fifths of the answer right yourself — the one you want is called iotop. Search for it in the extra repositories, it should be there.
There’s an iotop package in the universe repository available since Ubuntu 8.10 (Intrepid Ibex), so a sudo apt-get install iotop should do it.
htop » F2 » Columns » Active Columns » IO_RATE
Then sort by this column. Also you can add IO_READ_RATE and IO_WRITE_RATE columns and sort according to them.
This worked for me on the embedded Linux in a Synology box (where installing other packages isn’t really an option)
iotop is the counterpart to top that watches I/O usage information. If you want detailed information on the files opened by a process, or the list of files opened in a directory, or watch over files in the whole system, use lsof . lsof is quite versatile and provides information about open tcp, udp, NFS connections too.
Atop is an ASCII full-screen performance monitor that is capable of reporting the activity of all processes (even if processes have finished during the interval), daily logging of system and process activity for long-term analysis, highlighting overloaded system resources by using colors, etc. At regular intervals, it shows system-level activity related to the CPU, memory, swap, disks and network layers, and for every active process it shows the CPU utilization, memory growth, disk utilization, priority, username, state, and exit code.