nixCraft Poll

Topics

Linux: Find out how many file descriptors are being used

Posted by Vivek Gite [Last updated: August 21, 2007]

While administrating a box, you may wanted to find out what a processes is doing and find out how many file descriptors (fd) are being used. You will surprised to find out that process does open all sort of files:
=> Actual log file
=> /dev files
=> UNIX Sockets
=> Network sockets
=> Library files /lib /lib64
=> Executables and other programs etc

The best way to find out this information is use lsof command or explore /proc/PID directory for each running processes (including kernel processes), containing information about that process.

Step # 1 Find out program PID

Let us find out PID for mysqld process
# ps aux | grep mysqld
OR
# pidof mysqld
Output:

28290

Step # 2 List file opened by pid 28290

Use lsof command or /proc/PID file system to display fd lists:
# lsof -p 28290
OR
# cd /proc/28290/fd
# ls -l | less

You can count open file, enter:
# ls -l | wc -l

More about /proc/PID & procfs

/proc (or procfs) is a pseudo-file system that it is dynamically generated after each reboot. It is used to access kernel information. procfs is also used by Solaris, BSD, AIX and other UNIX like operating systems.

So now you know how many file descriptors are being used by a process. You will find more interesting stuff in /proc/PID directory. For example:

As you see, /proc is an essentials file system for admin work. Just browser through our previous article to get more information about /proc:

Other /proc related articles

I recommend reading /proc file system related document and lsof man page to get a better understanding about fd and files.

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. raj Says:

    hey thanks for quick n dirty procfs tutorial :)

  2. Bogdan Says:

    thx, that was really useful!quick question: anybody has an idea why the value displayed by ‘lsof -p {procid} | wc -l’ is different to that of ‘ls -l /proc/{procid}/fd | wc -l’. lsof is usually higher…?

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.