文件描述符耗尽的定位与修复
应用报 Too many open files、EMFILE,新连接无法建立,日志写入失败。accept()、open()、socket() 都可能返回错误。
ulimit -n
cat /proc/<pid>/limits | grep 'open files'
ls /proc/<pid>/fd | wc -l
lsof -p <pid> | wc -l
lsof -p <pid> | awk '{print $5}' | sort | uniq -c | sort -rn | head
cat /proc/sys/fs/file-nr
ss -s
systemd 服务:
systemctl show <service> -p LimitNOFILE
systemctl status <service>
ulimit -n 或 systemd LimitNOFILE 太小。inotify、epoll 等也占用描述符,监控工具会放大占用。lsof 看 fd 类型和增长趋势,确认是泄漏还是上限问题。finally 关闭、连接池有上限、复用长连接。ulimit -n,服务用 systemd LimitNOFILE,容器用 --ulimit nofile=。/etc/security/limits.conf 只影响登录会话,不一定会作用于 systemd 服务。/proc/<pid>/limits 已生效。lsof — 查看进程打开的文件ss — 套接字统计prlimit — 查看和设置进程资源限制systemctl — 服务限制合上页面,先回答,再展开对照。
/etc/security/limits.conf 可能对 systemd 服务无效?lsof -p <pid> 看 fd 类型和增长趋势。再对比 /proc/<pid>/fd 数量与 /proc/<pid>/limits,持续增长更像泄漏。LimitNOFILE,可用 systemctl show <service> -p LimitNOFILE 查看。/proc/<pid>/limits。确认 open files 已经是新值。