Tim Hastings - NonHostile (because there's no need)

Weblog and collection of geeky articles.

  Home :: Who? :: Contact :: Links :: Subscribe subscribe
A Very Busy Young LadyMeeting The FamilyLate Posted Christmas Pictures


A number of times I have found myself unable to access a file because some other process is writing to it.

Usually lock files which are used to prevent multiple instances of the same program from running at the same time, or to guard against multiple operations running at the same time that would interfere with each other.

The Linux command which comes to the rescue is called lsof which I guess is short for "list of open files"

It can be used in several different ways:

List All Open Files

    lsof

This will list all the God damn files open on your system. There will be lots of other interesting things listed which are not files, things like pipes and network sockets. This is because on Linux, everything is abstracted to a file, even the keyboard.

The output from lsof can be piped to grep to search for bits of file names.

    lsof | grep pony

This will perform a very crude search for any file name, path or process with anything to do with 'pony'

List All Open Files for a Given Process (by PID)

For example:

    lsof -p 12345

Lists all file handles currently held by process 12345

lsof headings

By default, the lsof output looks like this (without headings)

mysqld    30353     mysql  219u      REG        8,2 5976322292    6652117 /mnt/mysql/tagwalk/msg.MYD
mysqld    30353     mysql  220u      REG        8,2  672237472    6652170 /mnt/mysql/tagwalk/tag.MYD
mysqld    30353     mysql  221u      REG        8,2  672237472    6652170 /mnt/mysql/tagwalk/tag.MYD
mysqld    30353     mysql  222u      REG        8,2 5976322292    6652117 /mnt/mysql/tagwalk/msg.MYD
(cmd)     (pid)     (user) (handle)  (type) (device)(size/offset) (node)  (path)

From left-to-right, the columns are:

  • Command (first 9 characters)
  • Process ID (PID)
  • User
  • File Descriptor and Access Mode: r=read, w=write, u=read/write
  • Type: REG=Regular File, PIPE=Pipe, IPv4=TCP/IP socket (everything's a file remember!)
  • Device: In this case, I'm not sure what "8,2" means
  • File Size or Offset (if relevant)
  • Node: The node number of this file (an internal file system thing)
  • Name: The file name and path of the open file

Hope this helps!




0 comments, Linux, Thursday, October 22, 2009 20:08

Timeline Navigation for Linux posts
How to Install Node.js on Ubuntu (made 31 weeks later)
How to Find Which Process has a File Open on Linux (this post, made Thursday, October 22, 2009 20:08)
Now Witness the Power of This Fully Armed and Operational Debian NSLU2 (made 66 weeks earlier)


Comments

Post a Comment
Name:  Home page and email address are optional.
  Email addresses will not be displayed or spammed!
Remember these details
Email:
Home Page:
Comment:
Comments cannot contain HTML, URLs will be formatted into hyperlinks.
I reserve the right to remove any comments for any reason.