brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: /proc/sys/fs/file-nr shows how many file handles the whole Linux system has open

Linux
~/tips/tip-linux-proc-file-nr-open-handles

cat /proc/sys/fs/file-nr

See system wide open file usage next to its maximum in one line.

When programs start failing with Too many open files, the question is whether one process hit its own limit or the whole system ran out. One small file answers the second part.

cat /proc/sys/fs/file-nr

It prints three numbers. The kernel documentation calls them the number of allocated file handles, the number of allocated but unused handles, and the maximum. The middle one is always 0 on modern kernels, and the documentation stresses that this is not an error. So the first number against the third tells you how close the system is to its limit.

On most current systems the maximum is huge, often the largest 64 bit number, and you will never reach it. In that case a Too many open files error comes from the limit of a single process, which you can see in /proc/PID/limits or change for a service with LimitNOFILE= in its unit.

To find which process holds many files, count the entries in /proc:

for p in /proc/[0-9]*; do echo "$(ls $p/fd 2>/dev/null | wc -l) $(cat $p/comm 2>/dev/null)"; done | sort -rn | head

I’d run that with sudo, because otherwise you only see your own processes.

A count that keeps rising over days, while the load stays the same, points to a leak in one program, often sockets that are never closed. Raising fs.file-max only postpones that problem. Fix or restart the leaking program instead.

Further reading: Documentation for /proc/sys/fs/, proc_sys_fs(5) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link