How to check open files for a process

While debugging your program, you may encounter the message “Too many open files”. One way to fix the issue could be to review your code and check open, fopen, socket and pipe calls are matched with close and fclose calls, but with large projects this may be cumbersome.

A better way is to list the open files using the proc filesystem.
I’ll use VirtualBox program as an example since this is running in our server.

First, locate the process ID (PID):

pgrep VirtualBox
3901
3950

Then list the file descriptor opened for process 3901

sudo ls -l /proc/3901/fd
total 0
lrwx—— 1 root root 64 2010-10-05 14:52 0 -> /dev/pts/1
lrwx—— 1 root root 64 2010-10-05 14:52 1 -> /dev/pts/1
lr-x—— 1 root root 64 2010-10-05 14:52 10 -> pipe:[15825]
l-wx—— 1 root root 64 2010-10-05 14:52 11 -> pipe:[15825]
lr-x—— 1 root root 64 2010-10-05 14:52 12 -> pipe:[15829]
l-wx—— 1 root root 64 2010-10-05 14:52 13 -> pipe:[15829]
lrwx—— 1 root root 64 2010-10-05 14:52 14 -> socket:[15842]
lr-x—— 1 root root 64 2010-10-05 14:52 15 -> pipe:[15844]
lr-x—— 1 root root 64 2010-10-05 14:52 16 -> pipe:[15832]
l-wx—— 1 root root 64 2010-10-05 14:52 17 -> pipe:[15832]
l-wx—— 1 root root 64 2010-10-05 14:52 18 -> pipe:[15844]
lr-x—— 1 root root 64 2010-10-05 14:52 19 -> pipe:[15996]
lrwx—— 1 root root 64 2010-10-05 14:52 2 -> /dev/pts/1
l-wx—— 1 root root 64 2010-10-05 14:52 20 -> pipe:[15996]
lr-x—— 1 root root 64 2010-10-05 14:52 21 -> pipe:[16007]
l-wx—— 1 root root 64 2010-10-05 14:52 22 -> pipe:[16007]
lr-x—— 1 root root 64 2010-10-05 14:52 3 -> /opt/VirtualBox
lr-x—— 1 root root 64 2010-10-05 14:52 4 -> /opt/VirtualBox
lr-x—— 1 root root 64 2010-10-05 14:52 5 -> /opt/VirtualBox
lr-x—— 1 root root 64 2010-10-05 14:52 6 -> /opt/VirtualBox/components
lrwx—— 1 root root 64 2010-10-05 14:52 7 -> socket:[15822]
lr-x—— 1 root root 64 2010-10-05 14:52 8 -> pipe:[15824]
l-wx—— 1 root root 64 2010-10-05 14:52 9 -> pipe:[15824]

Jean-Luc started CNX Software in 2010 as a part-time endeavor, before quitting his job as a software engineering manager, and starting to write daily news, and reviews full time later in 2011.

Support CNX Software - Donate via PayPal, become a Patron on Patreon, or buy review samples
Subscribe
Notify of
guest
2 Comments
oldest
newest most voted
Xebeche
Xebeche
8 years ago

Hi, The “lsof” command do the same with more informations : # aptitude install lsof # lsof -p $(pgrep syslog) OMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rsyslogd 979 syslog cwd DIR 8,3 4096 2 / rsyslogd 979 syslog rtd DIR 8,3 4096 2 / rsyslogd 979 syslog txt REG 8,6 331192 2157 /usr/sbin/rsyslogd rsyslogd 979 syslog mem REG 8,3 88384 8397 /lib/x86_64-linux-gnu/libgcc_s.so.1 rsyslogd 979 syslog mem REG 8,3 47680 10327 /lib/x86_64-linux-gnu/libnss_nis-2.13.so rsyslogd 979 syslog mem REG 8,3 97248 9267 /lib/x86_64-linux-gnu/libnsl-2.13.so rsyslogd 979 syslog mem REG 8,3 35712 10041 /lib/x86_64-linux-gnu/libnss_compat-2.13.so rsyslogd 979 syslog mem REG 8,6 23544 2203… Read more »

Advertisements