How to display open file descriptors but not using lsof command

  • Hi I have read Here that lsof is not an accurate way of getting the number of File Descriptors that are currently open. He recommended to use this command instead

     cat /proc/sys/fs/file-nr

    While this command displays the number of FD's, how do you display the list of open file descriptors that the command above just counted?

    You probably want to know if your ulimit is exceeded, right? I blogged about this under; most importantly, the ulimit is a per-process restriction that you can find under /proc/PID/limits and instead of lsof I would use ls /proc/PID/fd to list the process' file descriptors.

  • There are two reasons lsof | wc -l doesn't count file descriptors. One is that it lists things that aren't open files, such as loaded dynamically linked libraries and current working directories; you need to filter them out. Another is that lsof takes some time to run, so can miss files that are opened or closed while it's running; therefore the number of listed open files is approximate. Looking at /proc/sys/fs/file-nr gives you an exact value at a particular point in time.

    cat /proc/sys/fs/file-nr is only useful when you need the exact figure, mainly to check for resource exhaustion. If you want to list the open files, you need to call lsof, or use some equivalent method such as trawling /proc/*/fd manually.

    Hi thanks for giving a good explanation Gilles. I tried ls /proc/*/fd and got all the open fd's at that time. Its producing an output with some color coding, I'll just have to look at the manual.

    @dimas `/proc/*/fd` directories contain symbolic links to the open files. For visual inspection, use `ls -l`. For automated treatment, use `readlink` to extract the link target.

    Just use ls -l but i'll experiment with readlink. I tried other /proc/PID/maps and other options as specified here Thanks again for the additional info.

    Note that file-nr outputs the amount of allocated filedescriptors, not the actual amount of used and opened ones.

    /proc/sys/fs/file-nr gives me 3872 (and two other numbers). How can this be the count of files I have open if ulimit -n shows me 1024?

    @ThorstenStaerk `file-nr` is the total number of open files across the system. `ulimit -n` sets the maximum number of files open by each process.

    @Gilles, do you have any reference for your statement? I looked into the man page and it explicitely states for -v that it is a per-process limitation but does NOT state this for -n.

    @ThorstenStaerk All settings of `setrlimit` (the system call underlying the `ulimit` shell command) are per-process. They affect only the process that makes the call (and indirectly the processes that it later forks).

    you are right, I verified this with a test program, pasted to

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM