Linux: Find All File Descriptors Used By a Process

by on July 1, 2011 · 1 comment· LAST UPDATED July 1, 2011

in

How can I find all the file descriptors used by a process such as httpd (Apache web server) or mysqld (MySQL Database server)?

You can use /proc file system or the lsof command to find all the file descriptors used by a process.

/proc File System Example

First, find out process ID using the ps command, enter:
# ps aux | grep processName
# ps aux | grep mysqld

Sample outputs:

root      3632  0.0  0.0  65944  1296 ?        S    Jun05   0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --user=mysql
mysql     3679  9.0  8.9 1393476 733756 ?      Sl   Jun05 3448:12 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-external-locking --socket=/var/lib/mysql/mysql.sock
root     44723  0.0  0.0  61188   820 pts/0    S+   17:48   0:00 grep mysqld

You can also use the pidof command as follows to find out the process id’s (pids) of the named programs:
# pidof processName
# pidof mysqld

Sample outputs:

3679

Note down the PID # 3679.

/proc/$pid/fd Directory Example

Type the following command:
# ls /proc/3679/fd
# ls -l /proc/3679/fd

Sample outputs:

total 0
lr-x------ 1 root root 64 Jul  1 13:09 0 -> /dev/null
l-wx------ 1 root root 64 Jul  1 13:09 1 -> /var/log/mysqld.log
lrwx------ 1 root root 64 Jul  1 13:09 10 -> socket:[14667]
lrwx------ 1 root root 64 Jul  1 13:09 100 -> /var/lib/mysql/blogdb/wp_term_relationships.MYI
lrwx------ 1 root root 64 Jul  1 13:09 101 -> /var/lib/mysql/blogdb/wp_postmeta.MYI
lrwx------ 1 root root 64 Jul  1 13:09 102 -> /var/lib/mysql/blogdb/wp_terms.MYI
lrwx------ 1 root root 64 Jul  1 13:09 103 -> /var/lib/mysql/orders/discussionread.MYD
lrwx------ 1 root root 64 Jul  1 13:09 104 -> /var/lib/mysql/orders/comments.MYI
lrwx------ 1 root root 64 Jul  1 13:09 105 -> /var/lib/mysql/orders/useractivation.MYI
lrwx------ 1 root root 64 Jul  1 13:09 106 -> /var/lib/mysql/orders/postparsed.MYI
lrwx------ 1 root root 64 Jul  1 13:09 107 -> /var/lib/mysql/orders/sigparsed.MYI
lrwx------ 1 root root 64 Jul  1 13:09 108 -> /var/lib/mysql/support/forum.MYI
......
.......
...
(output truncated)

lsof command Example

Type the command as follows:
# lsof -a -p {PID-HERE}
# lsof -a -p 3679

Sample outputs:

COMMAND  PID  USER   FD      TYPE             DEVICE     SIZE     NODE NAME
mysqld  3679 mysql  cwd       DIR                8,2     4096  7667720 /var/lib/mysql
mysqld  3679 mysql  rtd       DIR                8,2     4096        2 /
mysqld  3679 mysql  txt       REG                8,2  7721832 15185707 /usr/libexec/mysqld
mysqld  3679 mysql  mem       REG                8,2          13074466 /lib64/ld-2.5.so (path inode=13074445)
mysqld  3679 mysql  mem       REG                8,2          13074602 /lib64/libc-2.5.so (path inode=13074454)
mysqld  3679 mysql  mem       REG                8,2          13074603 /lib64/libdl-2.5.so (path inode=13074461)
mysqld  3679 mysql  mem       REG                8,2          13074608 /lib64/libm-2.5.so (path inode=13074464)
mysqld  3679 mysql  mem       REG                8,2          13074612 /lib64/libpthread-2.5.so (path inode=13074491)
mysqld  3679 mysql  mem       REG                8,2    85928 15193367 /usr/lib64/libz.so.1.2.3
mysqld  3679 mysql  mem       REG                8,2   247496 13074620 /lib64/libsepol.so.1
mysqld  3679 mysql  mem       REG                8,2    95464 13074623 /lib64/libselinux.so.1
mysqld  3679 mysql  mem       REG                8,2          13074615 /lib64/librt-2.5.so (path inode=13074500)
mysqld  3679 mysql  mem       REG                8,2          13074618 /lib64/libnsl-2.5.so (path inode=13074469)
mysqld  3679 mysql  mem       REG                8,2          13074633 /lib64/libcrypt-2.5.so (path inode=13074459)
mysqld  3679 mysql  mem       REG                8,2  1366272 13074653 /lib64/libcrypto.so.0.9.8e
mysqld  3679 mysql  mem       REG                8,2          13074655 /lib64/libresolv-2.5.so (path inode=13074496)
mysqld  3679 mysql  mem       REG                8,2    58400 13074610 /lib64/libgcc_s-4.1.2-20080825.so.1
mysqld  3679 mysql  mem       REG                8,2    10000 13074666 /lib64/libcom_err.so.2.1
mysqld  3679 mysql  mem       REG                8,2          15193399 /usr/lib64/libk5crypto.so.3.1 (path inode=15186140)
mysqld  3679 mysql  mem       REG                8,2     9728 13074658 /lib64/libkeyutils-1.2.so
mysqld  3679 mysql  mem       REG                8,2          15193402 /usr/lib64/libgssapi_krb5.so.2.2 (path inode=15186013)
mysqld  3679 mysql  mem       REG                8,2          15193397 /usr/lib64/libkrb5support.so.0.1 (path inode=15186147)
mysqld  3679 mysql  mem       REG                8,2          15193400 /usr/lib64/libkrb5.so.3.3 (path inode=15184571)
mysqld  3679 mysql  mem       REG                8,2   976312 15180443 /usr/lib64/libstdc++.so.6.0.8
mysqld  3679 mysql  mem       REG                8,2   315032 13074667 /lib64/libssl.so.0.9.8e
mysqld  3679 mysql  mem       REG                8,2          13074479 /lib64/libnss_files-2.5.so (path inode=13074476)
mysqld  3679 mysql    0r      CHR                1,3              4102 /dev/null
.....
..
...
mysqld  3679 mysql  234u      REG                8,2        0  7700867 /var/lib/mysql/order/subscribediscussion.MYD
mysqld  3679 mysql  238u      REG                8,2     2048  7700637 /var/lib/mysql/email/mailqueue.MYI
mysqld  3679 mysql  239u      REG                8,2    79060  7700593 /var/lib/mysql/email/mailqueue.MYD
mysqld  3679 mysql  340u      REG                8,2 17690624  7766217 /var/lib/mysql/blogdb/wp_posts.MYI

To see statistics on file descriptors used, enter:
# cat /proc/3679/net/sockstat
Sample outputs:

sockets: used 183
TCP: inuse 5 orphan 0 tw 0 alloc 9 mem 2
UDP: inuse 9 mem 1
UDPLITE: inuse 0
RAW: inuse 0
FRAG: inuse 0 memory 0

Please note that on older kernel such as those shipped with RHEL 5 or Suse 9 or Debian 4 or 5 use system wild statistics file descriptors file called /proc/net/sockstat:
# cat /proc/net/sockstat
Sample outputs:

sockets: used 455
TCP: inuse 146 orphan 0 tw 673 alloc 146 mem 9
UDP: inuse 14 mem 0
RAW: inuse 0
FRAG: inuse 0 memory 0

See also:

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 1 comment… read it below or add one }

1 Tom September 12, 2011 at 2:41 pm

It should be “system wide” not “system wild”. :)

Reply

Leave a Comment

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

Previous Faq:

Next Faq: