Find out open files of a process


Some one asked me yesterday how to find out the open files of a process. I could not answer it. But I knew it has something to do with the /proc folder, as I restored the apache access log once from the /proc folder after a user deleted the access log in the /var folder before. After a quick search, I find two ways to do it. I still take apache as a example. First, check the pid of it. Say it is 16138

Method 1: Check the /pro/$pid/fd folder

[root@centos-01 ~]# ls -l /proc/16138/fd
total 0
lr-x——. 1 root root 64 Aug 28 21:27 0 -> /dev/null
l-wx——. 1 root root 64 Aug 28 21:27 1 -> /dev/null
l-wx——. 1 root root 64 Aug 28 21:27 2 -> /var/log/httpd/error_log
lrwx——. 1 root root 64 Aug 28 21:27 3 -> socket:[57650]
lrwx——. 1 root root 64 Aug 28 21:27 4 -> socket:[57651]
lr-x——. 1 root root 64 Aug 28 21:27 5 -> pipe:[57673]
l-wx——. 1 root root 64 Aug 28 21:27 6 -> pipe:[57673]
l-wx——. 1 root root 64 Aug 28 21:27 7 -> /var/log/httpd/access_log
lr-x——. 1 root root 64 Aug 28 21:27 8 -> /dev/urandom

Method 2: Use the lsof command

[root@centos-01 ~]# lsof -p 16138
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
httpd   16138 root  cwd    DIR    8,2     4096      2 /
httpd   16138 root  rtd    DIR    8,2     4096      2 /
httpd   16138 root  txt    REG    8,2   354816 399332 /usr/sbin/httpd
httpd   16138 root  mem    REG    8,2     9488 399251 /usr/lib64/apr-util-1/apr_ldap-1.so
httpd   16138 root  mem    REG    8,2    27424 260638 /lib64/libnss_dns-2.12.so
httpd   16138 root  mem    REG    8,2    65928 260640 /lib64/libnss_files-2.12.so
httpd   16138 root  mem    REG    8,2    10416 399328 /usr/lib64/httpd/modules/mod_version.so
httpd   16138 root  mem    REG    8,2    27312 399287 /usr/lib64/httpd/modules/mod_cgi.so
httpd   16138 root  mem    REG    8,2    22992 399294 /usr/lib64/httpd/modules/mod_disk_cache.so
httpd   16138 root  mem    REG    8,2    10384 399324 /usr/lib64/httpd/modules/mod_suexec.so
httpd   16138 root  mem    REG    8,2    39664 399285 /usr/lib64/httpd/modules/mod_cache.so
httpd   16138 root  mem    REG    8,2    14648 399314 /usr/lib64/httpd/modules/mod_proxy_connect.so
httpd   16138 root  mem    REG    8,2    39632 399312 /usr/lib64/httpd/modules/mod_proxy_ajp.so
httpd   16138 root  mem    REG    8,2    31472 399316 /usr/lib64/httpd/modules/mod_proxy_http.so
httpd   16138 root  mem    REG    8,2    35600 399315 /usr/lib64/httpd/modules/mod_proxy_ftp.so
httpd   16138 root  mem    REG    8,2    27160 399313 /usr/lib64/httpd/modules/mod_proxy_balancer.so
httpd   16138 root  mem    REG    8,2    85328 399311 /usr/lib64/httpd/modules/mod_proxy.so
httpd   16138 root  mem    REG    8,2    60464 399319 /usr/lib64/httpd/modules/mod_rewrite.so
httpd   16138 root  mem    REG    8,2    14608 399323 /usr/lib64/httpd/modules/mod_substitute.so
httpd   16138 root  mem    REG    8,2    14608 399267 /usr/lib64/httpd/modules/mod_alias.so
httpd   16138 root  mem    REG    8,2    10448 399326 /usr/lib64/httpd/modules/mod_userdir.so
httpd   16138 root  mem    REG    8,2    14608 399321 /usr/lib64/httpd/modules/mod_speling.so
httpd   16138 root  mem    REG    8,2    10416 399266 /usr/lib64/httpd/modules/mod_actions.so
httpd   16138 root  mem    REG    8,2    10416 399293 /usr/lib64/httpd/modules/mod_dir.so
httpd   16138 root  mem    REG    8,2    31384 399310 /usr/lib64/httpd/modules/mod_negotiation.so
httpd   16138 root  mem    REG    8,2    10424 399329 /usr/lib64/httpd/modules/mod_vhost_alias.so
httpd   16138 root  mem    REG    8,2    52016 399290 /usr/lib64/httpd/modules/mod_dav_fs.so
httpd   16138 root  mem    REG    8,2    19032 399303 /usr/lib64/httpd/modules/mod_info.so
httpd   16138 root  mem    REG    8,2    35472 399284 /usr/lib64/httpd/modules/mod_autoindex.so
httpd   16138 root  mem    REG    8,2    22864 399322 /usr/lib64/httpd/modules/mod_status.so
httpd   16138 root  mem    REG    8,2    93328 399289 /usr/lib64/httpd/modules/mod_dav.so
httpd   16138 root  mem    REG    8,2    18800 399308 /usr/lib64/httpd/modules/mod_mime.so
httpd   16138 root  mem    REG    8,2    14576 399320 /usr/lib64/httpd/modules/mod_setenvif.so
httpd   16138 root  mem    REG    8,2    14608 399327 /usr/lib64/httpd/modules/mod_usertrack.so
httpd   16138 root  mem    REG    8,2    18896 399300 /usr/lib64/httpd/modules/mod_headers.so
httpd   16138 root  mem    REG    8,2    22960 399292 /usr/lib64/httpd/modules/mod_deflate.so
httpd   16138 root  mem    REG    8,2    10512 399297 /usr/lib64/httpd/modules/mod_expires.so
httpd   16138 root  mem    REG    8,2    27672 399309 /usr/lib64/httpd/modules/mod_mime_magic.so
httpd   16138 root  mem    REG    8,2    23120 399298 /usr/lib64/httpd/modules/mod_ext_filter.so
httpd   16138 root  mem    REG    8,2    10416 399296 /usr/lib64/httpd/modules/mod_env.so
httpd   16138 root  mem    REG    8,2    10440 399307 /usr/lib64/httpd/modules/mod_logio.so
httpd   16138 root  mem    REG    8,2    27200 399305 /usr/lib64/httpd/modules/mod_log_config.so
httpd   16138 root  mem    REG    8,2    43600 399302 /usr/lib64/httpd/modules/mod_include.so
httpd   16138 root  mem    REG    8,2    31192 399277 /usr/lib64/httpd/modules/mod_authnz_ldap.so
httpd   16138 root  mem    REG    8,2    88520 260674 /lib64/libz.so.1.2.3
httpd   16138 root  mem    REG    8,2   106160 393220 /usr/lib64/libsasl2.so.2.0.23
httpd   16138 root  mem    REG    8,2   240592 260681 /lib64/libnspr4.so
httpd   16138 root  mem    REG    8,2    18720 260682 /lib64/libplc4.so
httpd   16138 root  mem    REG    8,2    14560 260683 /lib64/libplds4.so
httpd   16138 root  mem    REG    8,2   154456 392670 /usr/lib64/libnssutil3.so
httpd   16138 root  mem    REG    8,2  1283920 393943 /usr/lib64/libnss3.so
httpd   16138 root  mem    REG    8,2   181136 393946 /usr/lib64/libsmime3.so
httpd   16138 root  mem    REG    8,2   242112 393947 /usr/lib64/libssl3.so
httpd   16138 root  mem    REG    8,2   110960 260650 /lib64/libresolv-2.12.so
httpd   16138 root  mem    REG    8,2    60512 260919 /lib64/liblber-2.4.so.2.5.6
httpd   16138 root  mem    REG    8,2   305984 260921 /lib64/libldap-2.4.so.2.5.6
httpd   16138 root  mem    REG    8,2    56144 399304 /usr/lib64/httpd/modules/mod_ldap.so
httpd   16138 root  mem    REG    8,2     6264 399279 /usr/lib64/httpd/modules/mod_authz_default.so
httpd   16138 root  mem    REG    8,2    10448 399278 /usr/lib64/httpd/modules/mod_authz_dbm.so
httpd   16138 root  mem    REG    8,2    10520 399280 /usr/lib64/httpd/modules/mod_authz_groupfile.so
httpd   16138 root  mem    REG    8,2    10424 399282 /usr/lib64/httpd/modules/mod_authz_owner.so
httpd   16138 root  mem    REG    8,2    10384 399283 /usr/lib64/httpd/modules/mod_authz_user.so
httpd   16138 root  mem    REG    8,2    10448 399281 /usr/lib64/httpd/modules/mod_authz_host.so
httpd   16138 root  mem    REG    8,2     6264 399275 /usr/lib64/httpd/modules/mod_authn_default.so
httpd   16138 root  mem    REG    8,2    10416 399274 /usr/lib64/httpd/modules/mod_authn_dbm.so
httpd   16138 root  mem    REG    8,2    10352 399272 /usr/lib64/httpd/modules/mod_authn_anon.so
httpd   16138 root  mem    REG    8,2    10456 399271 /usr/lib64/httpd/modules/mod_authn_alias.so
httpd   16138 root  mem    REG    8,2    10416 399276 /usr/lib64/httpd/modules/mod_authn_file.so
httpd   16138 root  mem    REG    8,2    31256 399270 /usr/lib64/httpd/modules/mod_auth_digest.so
httpd   16138 root  mem    REG    8,2    10448 399269 /usr/lib64/httpd/modules/mod_auth_basic.so
httpd   16138 root  mem    REG    8,2   383504 260616 /lib64/libfreebl3.so
httpd   16138 root  mem    REG    8,2    16304 260706 /lib64/libuuid.so.1.3.0
httpd   16138 root  mem    REG    8,2    19536 260630 /lib64/libdl-2.12.so
httpd   16138 root  mem    REG    8,2  1912432 260624 /lib64/libc-2.12.so
httpd   16138 root  mem    REG    8,2   142464 260648 /lib64/libpthread-2.12.so
httpd   16138 root  mem    REG    8,2   181792 399247 /usr/lib64/libapr-1.so.0.3.9
httpd   16138 root  mem    REG    8,2  1522840 260690 /lib64/libdb-4.7.so
httpd   16138 root  mem    REG    8,2   165264 260828 /lib64/libexpat.so.1.5.2
httpd   16138 root  mem    REG    8,2    40400 260628 /lib64/libcrypt-2.12.so
httpd   16138 root  mem    REG    8,2   146080 399250 /usr/lib64/libaprutil-1.so.0.3.9
httpd   16138 root  mem    REG    8,2   122040 260694 /lib64/libselinux.so.1
httpd   16138 root  mem    REG    8,2   181464 260826 /lib64/libpcre.so.0.0.1
httpd   16138 root  mem    REG    8,2   595800 260632 /lib64/libm-2.12.so
httpd   16138 root  mem    REG    8,2   154464 260617 /lib64/ld-2.12.so
httpd   16138 root  DEL    REG    0,4           57681 /dev/zero
httpd   16138 root  DEL    REG    0,4           57679 /dev/zero
httpd   16138 root    0r   CHR    1,3      0t0   4021 /dev/null
httpd   16138 root    1w   CHR    1,3      0t0   4021 /dev/null
httpd   16138 root    2w   REG    8,2      452 781949 /var/log/httpd/error_log
httpd   16138 root    3u  sock    0,6      0t0  57650 can’t identify protocol
httpd   16138 root    4u  IPv6  57651      0t0    TCP *:http (LISTEN)
httpd   16138 root    5r  FIFO    0,8      0t0  57673 pipe
httpd   16138 root    6w  FIFO    0,8      0t0  57673 pipe
httpd   16138 root    7w   REG    8,2        0 782554 /var/log/httpd/access_log
httpd   16138 root    8r   CHR    1,9      0t0   4026 /dev/urandom

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s