How to debug too many files open errors

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the unix category.

Last Updated: 2022-05-26

There are limits on how many open files you may have on unix-systems

When developing Project S, I got the following error:

failed to open stream - too many open files

How do we find out what our per-process file limit is?

First find the pid (e.g. with ps | grep php revealing, say, 1216).

Then view the /proc/{PID}/limits entry to show what the limit is on that running process

$ cat /proc/1216/limits

Limit                     Soft Limit           Hard Limit           Units
Max cpu time              unlimited            unlimited            seconds
Max file size             unlimited            unlimited            bytes
Max data size             unlimited            unlimited            bytes
Max stack size            8388608              unlimited            bytes
Max core file size        0                    unlimited            bytes
Max resident set          unlimited            unlimited            bytes
Max processes             7814                 7814                 processes

# Important bit
Max open files            1024                 4096                 files

Max locked memory         16777216             16777216             bytes
Max address space         unlimited            unlimited            bytes
Max file locks            unlimited            unlimited            locks
Max pending signals       7814                 7814                 signals
Max msgqueue size         819200               819200               bytes
Max nice priority         0                    0
Max realtime priority     0                    0
Max realtime timeout      unlimited            unlimited            us

So only 7814 files as a hard limit. That's not a lot, is it?

Regarding changing this limit, a lot of online tutorials use ulimit. This, however, is deprecated in favor of sysctl.

(sysctl is used to configure Kernel parameters at run time.)

The limit can thus be increased system-wide by editing the limits file /etc/sysctl.conf then running sysctl -p.

Man page extract for -p flag:

-p[FILE], --load[=FILE]
  Load  in  sysctl  settings  from the file specified or /etc/sysctl.conf if none given