Linux – How to deal with error: Too many open files in system

cronlinuxPHPshell

I am running a cron and output coming to my id and getting below as output:

/bin/sh: /usr/bin/php: Too many open files in system

Cron runs every 5 mins.

Have checked /var/logs/cron and cron is executed every 5 mins.

Sites are working fine on the server.

Best Answer

This means your system ran out of file handles, either in your php or in another application. Make sure to close any files you open to free file handles.

To the number of handles of every program running, use (as root):

for p in $(ps -A -o pid); do
    nh=$(ls /proc/$p/fd  2>/dev/null | wc -l) &&
    exe=$(readlink -f /proc/$p/exe  2>/dev/null) &&
    echo "$p ($exe): $nh"
done

For a more verbose output, use lsof.

You can also increase the number of possible system file handles by modyfing /etc/security/limits.conf (which effects changes to /proc/sys/fs/file-max), and decrease it for the current terminal session with ulimit -n.