问题起因

周末在家瘫着突然接到反馈网站访问打开直接500错误,然后上去排查,发现很多服务启动不了,手动起服务提示too many open files,这句提示的原因是程序打开的文件/socket连接数量超过系统设定值。

登录服务器第一件事,先看下当前机器文件打开的最大限制(open files这行)

我这里是65535说明已经改过了,默认是1024

[root@10-23-52-95 rsyslog]# ulimit -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 95711
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 65535
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 65535
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

当然也可以用这种方式也可以:
[root@10-23-52-95 system]# cat /proc/sys/fs/file-max
65535

如果限制是1024可以改下,然后重启机器

[root@10-23-52-95 ~]# tail -n 10 /etc/security/limits.conf
End of file
hard nofile 1000000
soft nofile 1000000
soft core unlimited
soft stack 10240
soft nproc 65535
hard nproc 65535
soft nofile 65535
hard nofile 65535

看下当前系统具体占用了多少文件描述符

第一列是当前打开的,第三列是系统限制的最大打开数
[root@10-23-52-95 system]#  cat /proc/sys/fs/file-nr
9184 0 65535
那接下来查下是哪些进程占光的

查看打开文件句柄数的进程id排行 左边是数量,右边是进程ID,找到用的最多的进程,然后ps aux |grep 1661  就可以看到罪魁祸首了,再定位下具体进程就可以解决了

[root@10-23-52-95 ~]# lsof -n|awk '{print $2}'|sort|uniq -c|sort -nr|more
36894 1661
20618 29431
16716 29231

查看某一进程的文件描述符打开数

[root@10-23-52-95 ~]# lsof  | grep 29431