How to Find Which Process Uses Too Many Open Files in Linux

Quick Linux Tip #81:

Question: My server hits "too many open files" errors. Which process is the culprit?

Try: sudo lsof | awk '{print $2}' | sort | uniq -c | sort -rn | head -10

Info: lsof lists open file descriptors across processes. Grouping the output by PID counts open handles and helps identify processes consuming large numbers of file descriptors before they reach their limits in /proc/PID/limits.

Examples:

  • $ sudo ls /proc/*/fd 2>/dev/null | wc -l  # Count system-wide open file descriptors
  • $ sudo lsof -p 2456 | wc -l  # Count open files for a specific PID
  • $ sudo prlimit --pid 2456 --nofile=65536:65536  # Dynamically raise a process file limit

Note: Check the current process limits with cat /proc/PID/limits. You can dynamically adjust limits with prlimit or configure persistent limits through /etc/security/limits.conf. High-concurrency servers may require significantly higher file descriptor limits than the traditional 1024 soft limit.




LinuxTeck.com
linuxteck:~$ sudo lsof | awk '{print $2}' | sort | uniq -c | sort -rn | head -10
   4523 2456
   2341 3456
   1876 4567
   1234 5678
    892 1234
    734 6789
    567 7890
    432 8901

linuxteck:~$ ps -p 2456 -o pid,user,comm,args
    PID USER     COMMAND    COMMAND
   2456 mysql    mysqld     /usr/sbin/mysqld

linuxteck:~$ cat /proc/2456/limits | grep 'open files'
Max open files            1024                 4096                 files
linuxteck:~$

PREVIOUS ARTICLE Quick Linux Tip #80: How to Find Large Files Modified Recently in Linux NEXT ARTICLE Quick Linux Tip #82: How to Preserve File Timestamps with Rsync on Linux
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.