Lsof

From LQWiki
Jump to navigation Jump to search

lsof is a command used to list the open files on a system, in use by other processes.

Uses for lsof

Discovering why "device is busy"

It can be useful to find the cause of a "device is busy" error resulting from trying to unmount a disk. For each open file, lsof gives the process and user that has the file open. Many files like the standard libraries, will be listed more than once.

To determine the cause of device busy errors trying to unmount, for example, /mnt/cdrom, issue:

$ su
# lsof | grep /mnt/cdrom

will retrieve all the processes that have open files on the device. For example, if fam has open files on that device, the process needs to be stopped. If the device carries a writable file system, it may be advisable to quit whichever program is using the filesystem in order to make sure files are written to successfully. (Note that you need to use lsof as superuser. Although lsof will work for normal users, it won't show all the processes in question unless used by root.)

Discovering why "Could not bind to port"

Similarly, you can use lsof to find out which processes are making use of a particular port. This is useful for when you get "could not bind to port" messages.

$ su
# lsof -i:80 # show processes using port 80

(Again, notice the use of su.)

External links