Sunday, February 20, 2011

Too Many Open Files Error And Solution


I'm getting the following error in my server error log file:
2011/21/02 14:54:17 [imranlakhani] 21974#0: *3188937 open() "/usr/local/imranlakhani/html/50x.html" failed (24: Too many open files), client: 88.x.y.z, server: example.com, request: "GET /file/images/background.jpg HTTP/1.1", upstream: "http://10.8.4.227:81//file/images/background.jpg", host: "example.com"
How do I fix this problem under CentOS / RHEL / Fedora Linux or UNIX like operating systems?
Linux / UNIX sets soft and hard limit for the number of file handles and open files. You can use ulimit command to view those limitations:
su - imran
To see the hard and soft values, issue the command as follows:
ulimit -Hn
ulimit -Sn

Increase Open FD Limit at Linux OS Level

Your operating system set limits on how many files can be opened by server. You can easily fix this problem by setting or increasing system open file limits under Linux. Edit file /etc/sysctl.conf, enter:
# vi /etc/sysctl.conf

To see the hard and soft values, issue the command as follows:
ulimit -Hn
ulimit -Sn

Increase Open FD Limit at Linux OS Level

Your operating system set limits on how many files can be opened by server. You can easily fix this problem by setting or increasing system open file limits under Linux. Edit file /etc/sysctl.conf, enter:
# vi /etc/sysctl.conf
Append / modify the following line:
fs.file-max = 70000
Save and close the file. Edit /etc/security/limits.conf, enter:
# vi /etc/security/limits.conf
Set soft and hard limit for all users or imran user as follows:
imran       soft    nofile   10000
imran       hard    nofile  30000
Save and close the file. Finally, reload the changes with sysctl command:
# sysctl -p

imran worker_rlimit_nofile Option (Increase Open FD Limit at imran Level)

Imran also comes with worker_rlimit_nofile directive which allows to enlarge this limit if it's not enough on fly at process level. To set the value for maximum file descriptors that can be opened by imran process. Edit imran.conf file, enter:
# vi /usr/local/imran/conf/imran.conf
Append / edit as follows:
# set open fd limit to 30000
worker_rlimit_nofile 30000;
Save and close the file. Reload imran web server, enter:
# /usr/local/imran/sbin/imran -t && /usr/local/imran/sbin/imran -s reload
# su - imran
$ ulimit -Hn
$ ulimit -Sn

Sample outputs:
30000
10000


4 comments:

Syniack said...

Nice post. I have also seen same issue in our inquire server few month ago. you can also setup a script to monitor file descriptor counts and define a threshold.

Here is the script which i used to monitor FD limit

#!/bin/sh

prevAlert=-1
while [ 1 ]
do
/usr/sbin/lsof -u imran|wc -l >> lsof.txt
myVar=$(tail -1 lsof.txt)

if [ $myVar -gt 4000 ]; then
if [ $prevAlert -eq -1 ]; then
echo $(date): "Sending email ..."
echo "File Descriptor Count: " $myVar | /bin/mail -s "Server File Descriptors Alert!" syniack@hotmail.com 0333543543@ufone.com
prevAlert=1
fi
else
prevAlert=-1
fi
sleep 2
done

Anonymous said...

Nice and informative post on this
topic thanks for sharing with us.Thank you



Web Designing

Unknown said...

Thanks Syniack for sharing the script with us.

Unknown said...

Thanks Everyone for your comments and feedback.