LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Find open files in the system (https://www.linuxquestions.org/questions/linux-newbie-8/find-open-files-in-the-system-4175467988/)

procfs 07-01-2013 06:19 AM

Find open files in the system
 
Hi, I know this is a dumb question :(

I am trying to find the number of open files per process, and I found few commands but each gives deferrent values. below are the commands

ls /proc/<pid>/fd/*

and

lsof -p <PID>

Can some one tell me what I am doing wrong or am I reading the out put wrong

Thanks and Best Regards

eahmedshendy 07-01-2013 07:23 AM

Hi procfs,

You want to execute line to output the number of open files per process like this
Code:

4 yum
6 http
1 vim
.
.

Or like this
Code:

[root@router scripts]# ps aux | grep -i yum | wc -l
2
[root@router scripts]# ps aux | grep -i vim | wc -l
4


linuxzilla.com 07-01-2013 09:29 AM

Quote:

Originally Posted by eahmedshendy (Post 4981674)
Hi procfs,

You want to execute line to output the number of open files per process like this
Code:

4 yum
6 http
1 vim
.
.

Or like this
Code:

[root@router scripts]# ps aux | grep -i yum | wc -l
2
[root@router scripts]# ps aux | grep -i vim | wc -l
4


Wawoo, thanks, I didn't know that before & I'll definitely save these commands with me. I think that command ps aux | grep -i vim | wc -l will bring more cooler results.

unSpawn 07-02-2013 12:42 PM

Quote:

Originally Posted by procfs (Post 4981651)
Hi, I know this is a dumb question :(
I am trying to find the number of open files per process

Definitely not a dumb question!


Quote:

Originally Posted by eahmedshendy (Post 4981674)
Code:

[root@router scripts]# ps aux | grep -i yum | wc -l

Sorry, that's nice but it's wrong. Actually the OP already gave the clue:
Code:

_lsof() { [ $# -eq 1 ] && lsof -Pwln -a -d '^cwd' -a -p $(pgrep -d, $1) -Fi|sort -u|wc -l || echo "${FUNCNAME}: wrong."; }
Here, let me clarify:
Code:

~]# cmd1() { \ps aux|grep -i httpd|wc -l; }
 ~]# time cmd1
7

real    0m0.026s
user    0m0.015s
sys    0m0.016s

 ~]# cmd2() { lsof -Pwln -a -d '^cwd' -a -p $(pgrep -d, httpd) -Fi|sort -u|wc -l; }
 ~]# time cmd2
79

real    0m0.082s
user    0m0.016s
sys    0m0.072s


procfs 07-04-2013 05:20 AM

Hi Thank you Guys for the reply, but I am bit confused about the command that unSpawn has mentioned :(

Kind Regards

Madhu Desai 07-04-2013 06:46 AM

Quote:

Originally Posted by procfs (Post 4981651)
Hi, I know this is a dumb question :(
I am trying to find the number of open files per process, and I found few commands but each gives deferrent values. below are the commands
ls /proc/<pid>/fd/*
and
lsof -p <PID>
Can some one tell me what I am doing wrong or am I reading the out put wrong
Thanks and Best Regards

I think its a valid question. I always used lsof whenever I was unable to unmount file system, so that I could see what files are still used by that file system. I never used /proc/<pid>/fd.

So it seems, both lsof and /proc/<pid>/fd bring different results for same pid.

I did little search in internet and it seems the difference between output from lsof and /proc/<pid>/fd/ are because lsof also displays other file descriptors and memory mapped .so files, which are mapped at /proc/<pid>/maps.

Code:

$ pidof firefox
3093

$ lsof -p 3093
COMMAND  PID  USER  FD  TYPE            DEVICE SIZE/OFF    NODE NAME
firefox 3093 madhu  cwd    DIR              253,3    4096  2883585 /home/madhu
firefox 3093 madhu  rtd    DIR              253,1    4096        2 /
firefox 3093 madhu  txt    REG              253,1    94072  661414 /usr/lib64/firefox/firefox
firefox 3093 madhu  mem    REG              253,1  156872  661248 /lib64/ld-2.12.so
firefox 3093 madhu  mem    REG              253,1    22536  661252 /lib64/libdl-2.12.so
firefox 3093 madhu  mem    REG              253,1  1922152  661249 /lib64/libc-2.12.so
firefox 3093 madhu  mem    REG              253,1  145720  661250 /lib64/libpthread-2.12.so
firefox 3093 madhu  mem    REG              253,1  598680  661283 /lib64/libm-2.12.so
firefox 3093 madhu  mem    REG              253,1    47064  661251 /lib64/librt-2.12.so

<TRUNCATED>

$ lsof -p 3093 | grep -v '^COMMAND' | wc -l
303

$ ls -l /proc/3093/fd/*
lr-x------. 1 madhu madhu 64 Jul  4 11:17 /proc/3093/fd/0 -> /dev/null
lrwx------. 1 madhu madhu 64 Jul  4 11:17 /proc/3093/fd/1 -> /home/madhu/.xsession-errors
lrwx------. 1 madhu madhu 64 Jul  4 11:17 /proc/3093/fd/10 -> pipe:[20623]
lrwx------. 1 madhu madhu 64 Jul  4 16:37 /proc/3093/fd/101 -> /home/madhu/.mozilla/firefox/yu5ujt41.default/downloads.sqlite
lrwx------. 1 madhu madhu 64 Jul  4 16:37 /proc/3093/fd/102 -> socket:[42045]
lrwx------. 1 madhu madhu 64 Jul  4 11:17 /proc/3093/fd/11 -> socket:[20624]
lrwx------. 1 madhu madhu 64 Jul  4 11:17 /proc/3093/fd/12 -> /home/madhu/.mozilla/firefox/yu5ujt41.default/.parentlock
lrwx------. 1 madhu madhu 64 Jul  4 11:17 /proc/3093/fd/13 -> anon_inode:[eventpoll]
lrwx------. 1 madhu madhu 64 Jul  4 11:17 /proc/3093/fd/14 -> socket:[20630]
lrwx------. 1 madhu madhu 64 Jul  4 11:17 /proc/3093/fd/15 -> socket:[20631]

<TRUNCATED>

$ ls -l /proc/3093/fd/* | wc -l
79

So, as you can see lsof counts to 303, while /proc//fd counts to 79

Here is the list of File Descriptors
Quote:

cwd - current working directory
Lnn - library references (AIX)
err - FD information error (see NAME column)
jld - jail directory (FreeBSD)
ltx - shared library text (code and data)
Mxx - hex memory-mapped type number xx
m86 - DOS Merge mapped file
mem - memory-mapped file
mmap - memory-mapped device
pd - parent directory
rtd - root directory
tr - kernel trace file (OpenBSD)
txt - program text (code and data)
v86 - VP/ix mapped file
I put the File Descriptors in a file, and when used with grep, both are almost same now.
Code:

$ cat fds
cwd
Lnn
err
jld
ltx
Mxx
m86
mem
mmap
pd
rtd
tr
txt
v86


$ lsof -p 3093 | grep -v '^COMMAND' | grep -ivf fds | wc -l
84

$ ls -l /proc/3093/fd/* | wc -l
79

Almost Equal!!!

justjustin 07-05-2013 01:46 PM

This is some great info! Never knew pidof existed.

procfs 07-09-2013 04:27 AM

Hi this is great thank you mddesai, and for all for replying

Best Regards


All times are GMT -5. The time now is 12:59 AM.