Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I knows I can use the command

lsof -p xxxx | wc -l

to know the count of opened files op a process, it works, but however, it is just too inefficient. I have some server process which have too many socket files, the wc -l method never return the result. So, what is the efficient way to know how many files opened on a process?

Thanks.

share|improve this question
add comment

3 Answers 3

There is a proc interface for open files: /proc/PID/fd/. It is a directory of symlinks. Any open sockets will appear to be linked to a file named "socket:[INODE NUMBER]"

share|improve this answer
add comment

Use ps -C <progname> -o pid= | wc -l where progname is the name of the process in a ps-listing.

share|improve this answer
1  
That gives a count of processes not the count of open files. –  Dennis Williamson Jun 10 '10 at 19:07
    
Sorry, my explanation wasn't specific enough. As described by Andrew E. Falcon, these are file descriptor PIDs. You can see which file is behind a PID by running it that way: for pid in $(ps -C <progname> -o pid=); do ls -l /proc/$pid/fd; done –  weeheavy Jun 14 '10 at 8:34
add comment

Some context (use-case scenarios) may provide better direction for answers.

Documentation of lsof describes a few methods that it calls that may block for undesirable amounts of time. I would suggest glancing over the BLOCKS AND TIMEOUTS section of the lsof man page.

You might try the following and see if it provides desirable results:

lsof -bwp $pid | wc -l
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.