| 场景 | Too many open files |
|---|---|
| 组合 | /proc/PID/fd + lsof + ulimit/prlimit |
量化现状
量化现状
pid=$(pgrep -f 'app-server' | head -1)
ls /proc/$pid/fd | wc -l # 当前 fd 数
cat /proc/$pid/limits | grep -i files # 上限
lsof -p $pid | awk '{print $5}' | sort | uniq -c | sort -rn | head # 按类型分布找泄漏
找泄漏
lsof -p $pid | grep -c 'TCP' # 连接数是否异常增长
lsof -p $pid | awk '{print $9}' | sort | uniq -c | sort -rn | head # 集中在哪个文件
watch -n 5 'ls /proc/'$pid'/fd | wc -l' # 观察是否持续上涨临时缓解
临时缓解
prlimit --nofile=65535:65535 --pid $pid # 在线提限(治标)fd 持续增长=泄漏,提限只是拖时间;连接池/文件未 close 的代码问题该修还得修。