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]

Support CNX Software - Donate via PayPal or become a Patron on Patreon

2
Leave a Reply

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
cnxsoftXebeche Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Xebeche
Guest
Xebeche

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 /usr/lib/rsyslog/imklog.so
rsyslogd 979 syslog mem REG 8,6 15312 35164 /usr/lib/rsyslog/imuxsock.so
rsyslogd 979 syslog mem REG 8,3 51728 10180 /lib/x86_64-linux-gnu/libnss_files-2.13.so
rsyslogd 979 syslog mem REG 8,6 23264 24269 /usr/lib/rsyslog/lmnet.so
rsyslogd 979 syslog mem REG 8,3 1642216 8025 /lib/x86_64-linux-gnu/libc-2.13.so
rsyslogd 979 syslog mem REG 8,3 31752 10446 /lib/x86_64-linux-gnu/librt-2.13.so
rsyslogd 979 syslog mem REG 8,3 14696 8628 /lib/x86_64-linux-gnu/libdl-2.13.so
rsyslogd 979 syslog mem REG 8,3 140254 10422 /lib/x86_64-linux-gnu/libpthread-2.13.so
rsyslogd 979 syslog mem REG 8,3 96816 14592 /lib/x86_64-linux-gnu/libz.so.1.2.3.4
rsyslogd 979 syslog mem REG 8,3 141088 7208 /lib/x86_64-linux-gnu/ld-2.13.so
rsyslogd 979 syslog 0u unix 0xffff8801313f6700 0t0 9368 /dev/log
rsyslogd 979 syslog 1w REG 8,3 6108 537 /var/log/syslog
rsyslogd 979 syslog 2w REG 8,3 353475 2714 /var/log/auth.log
rsyslogd 979 syslog 3u unix 0xffff8801313f6080 0t0 9392 /var/spool/postfix/dev/log
rsyslogd 979 syslog 4r REG 0,3 0 4026532013 /proc/kmsg
rsyslogd 979 syslog 5w REG 8,3 506591 2675 /var/log/kern.log*

I hope this help.

Xeb.