List Open Files

 in

If you try to unmount a partition and get a message like this:

# umount /media/usbdisk/
umount: /media/usbdisk: device is busy

use the lsof command to find out what programs are using what files:

# lsof /media/usbdisk/
COMMAND  PID USER  FD TYPE DEVICE SIZE NODE NAME
bash    6925 root cwd  DIR   8,17 4096    1 /media/usbdisk/
xmms    6979 root cwd  DIR   8,17 4096    1 /media/usbdisk/

This shows that the programs bash and xmms are using the device. For an even clearer picture, use the device name rather than the mountpoint:

# lsof /dev/sdb1
COMMAND  PID USER  FD TYPE DEVICE    SIZE NODE NAME
bash    6925 root cwd  DIR   8,17    4096    1 /media/usbdisk
xmms    6979 root cwd  DIR   8,17    4096    1 /media/usbdisk
xmms    6979 root  8r  REG   8,17 2713101  377 /media/usbdisk/a.mp3

You either can wait until those processes exit or terminate them manually.

______________________

Jagadish Kavuturu

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

That's crazy! but what the hey, sometimes we're lazy :)

maskedfrog's picture

Thanx for the tip Anthony,
THAT will come in handy. The times I've waited for a cd to umount
and waited...and waited...and gave up and rebooted :-(
More apt to use in the examples shown though, waiting for a process to finish and don't want to forget to umount so just do it the "lazy" way.
Uptimes should shoot thru the roof now :-))

Slow

Hatta's picture

In my experience 'lsof' is extremely slow. So slow that I can log off and log back on before I'd expect lsof to finish. Are there any tricks to speed it up to the point that it's usable?

lsof is NOT slow at all.

VIKAS's picture

lsof is NOT slow at all.
I believe there would be other problems with your machine.

A very powerful command

SteveC's picture

The lsof command is far more powerful than the article implies. It's worth spending some time reading the documentation. My most common use is to locate connections with the -i option:

# all tcp connections
lsof -i tcp

# connections to mail server on 10.2.3.4
lsof -i 10.2.3.4:25

# What's connected to my PostgreSQL server via TCP/IP?
lsof -i :5432

# What is my browser connected to?
lsof -i :80

Killing with fuser

Anonymous's picture

If
    lsof +D [device]
shows nothing

you can use
    fuser -m [device]
to show processes that say that are using the device.

and you can use
    fuser -km [device]
to directly kill them all

Doesn't always work

Ivan V's picture

Unfortunately, many times, neither lsof nor fuser have worked for me (the device says busy but nothing comes up)...

The only solution in those cases is for me to logoff or try to find a possible culprit.

Thanks! That's a cool trick!

Durand's picture

Thanks! That's a cool trick!

Thanks, a much needed tip.

Anonymous's picture

Thanks, a much needed tip.

Nice tip

vinit's picture

Thanks for the gr8 tip.

Short and to the point

Anonymous's picture

Short and to the point Howto. Nice.

Don't forget fuser!

Devin's picture

You can also use fuser if you know which directory or file is open and you want to find the offending process.

fuser [file]

You can also use the "lazy

Anthony Lawrence's picture

You can also use the "lazy umount" - see http://aplawrence.com/Linux/lazy-unmount.html for examples.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState