"Too many open files" 错误表明系统或某个进程达到了打开文件描述符的限制。在Linux中,每个进程可以打开的文件数量是有限制的。
# 查看当前限制
ulimit -n
# 临时提高限制(会话有效)
ulimit -n 65535
编辑 /etc/sysctl.conf
文件:
sudo vi /etc/sysctl.conf
添加或修改以下行:
fs.file-max = 2097152
应用更改:
sudo sysctl -p
编辑 /etc/security/limits.conf
文件:
sudo vi /etc/security/limits.conf
添加以下内容(将username
替换为实际用户名或使用*
表示所有用户):
username soft nofile 65535
username hard nofile 65535
对于systemd管理的服务,创建或编辑服务配置文件:
sudo systemctl edit servicename.service
添加以下内容:
[Service]
LimitNOFILE=65535
然后重新加载并重启服务:
sudo systemctl daemon-reload
sudo systemctl restart servicename.service
# 查看系统总打开文件数
cat /proc/sys/fs/file-nr
# 查看特定进程打开的文件
lsof -p PID
ls -l /proc/PID/fd | wc -l
# 按打开文件数排序所有进程
lsof | awk '{print $1}' | sort | uniq -c | sort -n -r | head
# 查看当前进程的限制
cat /proc/PID/limits | grep "Max open files"
# 查看系统全局限制
cat /proc/sys/fs/file-max
通过以上步骤,您应该能够解决"Too many open files"的问题。如果问题仍然存在,可能需要进一步调查应用程序是否存在文件描述符泄漏问题。