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
- /proc/filesystems: Find out what filesystems supported by kernel
- Howto: Linux detect or find out a dual-core cpu
- Linux display CPU information - number of CPUs and their speed
- How to Scan new LUNs on Linux with QLogic driver
- Linux command to gathers up information about a Linux system
- Linux increase the maximum number of open files or file descriptors
- How to display or show information about a Linux Kernel module or drivers
- Linux scan wireless card for information
- Linux disable or drop / block ping packets all together
- How do I find out if my server CPU can run a 64 bit kernel version (apps) or not?
- Display Linux kernel slab cache information in real time
- Making changes to /proc filesystem permanently
- Howto rebuilding a RAID array after a disk fails
- Find out if my server is capable of running para-virtualized guest ( PAE support )
- Linux configure Network Address Translation or NAT
- Howto: Linux see new fiber channel attached disk LUNs without rebooting
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:
- Quick tip: Tell what hardware is connected via USB to my Linux desktop
- Understanding UNIX / Linux filesystem Superblock
- NFS Stale File Handle error and solution
- Programming C: Find out name of a terminal
- Understanding UNIX / Linux file system
Discussion on This Article:
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!
Tags: dev_files, executables, fd, grep_command, kernel, library_files, lsof_command, ls_command, network_sockets, pidof_command, processes, unix_sockets



hey thanks for quick n dirty procfs tutorial
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…?