Linux: Find out how many file descriptors are being used

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:

  • /proc/PID/cmdline : process arguments
  • /proc/PID/cwd : process current working directory (symlink)
  • /proc/PID/exe : path to actual process executable file (symlink)
  • /proc/PID/environ : environment used by process
  • /proc/PID/root : the root path as seen by the process. For most processes this will be a link to / unless the process is running in a chroot jail.
  • /proc/PID/status : basic information about a process including its run state and memory usage.
  • /proc/PID/task : hard links to any tasks that have been started by this (the parent) process.

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 read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 6 comments… read them below or add one }

1 raj 08.21.07 at 8:35 pm

hey thanks for quick n dirty procfs tutorial :)

2 Bogdan 03.28.08 at 4:00 pm

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…?

3 Jimi 02.03.09 at 7:04 pm

Thanks man, this was really useful.

4 Julien 02.04.09 at 10:12 pm

Thank you. I had a daemon that stopped functioning correctly six hours after I started it. Turns out, I had a file descriptor leak. It had never even crossed my mind until I put 2 and 2 together. This confirmed it. It’s all fixed now, thanks to you!

5 kunal 02.18.09 at 8:56 am

Can’t we use lsof | wc -l for the same

6 yep 02.25.09 at 5:43 pm

Hey Bogdan,

I think lsof gives you a listing of open files, while the /proc/PID/fd directory contains a listing offile descriptors… not necesarily the same thing. Youmay want to check out the following article http://www.netadmintools.com/art295.html.

Leave a Comment

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

Tagged as: , , , , , , , , , , ,

Previous post: Howto: Create Shared Storage on Suse Linux using OCFS2 and Xen Virtualization

Next post: Howto: Redhat Enterprise Linux SELinux policy guide