Troubleshoot Too Many Open Files in a Linux Service
Before you begin
- Applies to:Linux hosts running the affected service under systemd.
- Where to start:Open a shell on the host where the error appeared.
- What you need:Record the exact error and its time. Have access to the service journal and the affected worker under /proc. Use the real unit name and worker PID. The main PID may belong to a supervisor.
Guide tools
Troubleshooting Steps
Identify the failing process and error
Start hereCheck the service journal. Where the application reports errno, EMFILE points to a per-process limit, while ENFILE points to a system-wide file table limit. Match the timestamp and worker to the request that failed.
sudo journalctl -u example.service --since '30 minutes ago' --no-pagersystemctl show example.service -p MainPID -p LimitNOFILE -p LimitNOFILESoftInspect the running worker's effective limits
The configured unit limit can differ from a process that changes its own limits. Compare the soft and hard Max open files values. A shell’s ulimit describes that shell and its descendants.
What you should see · The running worker has a soft and hard Max open files value in its process limits.
If you see something else · If the PID no longer exists, get the current worker PID before measuring. If it is still running, compare its descriptor count against the soft limit in Step 3 before changing it.
pid=1234sudo grep -F 'Max open files' "/proc/$pid/limits"Sample descriptors under normal load
Count the current descriptors and compare several samples from the same worker. A worker can exit or reuse descriptors while you inspect it. Use lsof if available to distinguish files, sockets, or other descriptor types. Inspect FD entries rather than counting all lsof rows.
pid=1234(set -o pipefail; sudo ls -1 -- "/proc/$pid/fd" | wc -l)sudo lsof -nP -p "$pid"Separate system-wide pressure from one worker
When several unrelated services report ENFILE, review allocated kernel file handles and the configured maximum. file-nr and file-max describe system-wide state. Compare the observed numbers and kernel logs during the failure rather than inferring saturation from one process count.
cat /proc/sys/fs/file-nr /proc/sys/fs/file-maxChoose a correction based on growth
If descriptors grow with no corresponding workload growth, investigate application or connection cleanup with its owner. If normal workload reaches a justified process limit, select a vendor-supported soft and hard limit, record the existing setting, and use an approved service-specific drop-in. A running process needs an approved restart to inherit the revised setting.
Verify the worker and the original workload
VerifyRecheck the replacement worker’s effective values, descriptor trend, and job results during comparable load. If a change causes regressions, restore the prior drop-in, reload systemd, and restart through the agreed maintenance workflow.
Decide What to Do Next
| Finding | Next action |
|---|---|
| One worker approaches its soft limit | Compare descriptor types and behavior under stable load. |
| Several services report ENFILE | Compare the system-wide file table with kernel messages. |
References
Your feedback keeps these production procedures accurate and battle-tested.