最近使用springboot,在高並發下出現了一個問題:
服務的進程還跑着,端口缺不再監聽了,報錯如下:
Too many open files in system
原來是開的系統文件太高了
通過以下命令可以查看系統文件總上限:
cat /proc/sys/fs/file-max
cat /proc/sys/fs/file-nr
Centos7下,以下命令修改文件上限:
實時修改:
sysctl -w fs.file-max=500000
永久修改:
vi /etc/sysctl.conf
增加內容
fs.file-max=500000
刷新
sysctl -p
通過以下命令可以查看單個用戶limit
ulimit -n
Centos7下,以下命令修改單個用戶文件上限:
#實時修改 ulimit -n 655350 #永久修改 vi /etc/security/limits.d/20-nproc.conf
添加內容:
#nproc
* soft nproc 655350
root soft nproc unlimited
* hard nproc 655350
root hard nproc unlimited
#nofile
* soft nofile 655350
* hard nofile 655350
centos-7 新增了
/etc/security/limits.d/20-nproc.conf
文件,並且該文件會覆蓋/etc/security/limits.conf
的配置參數
參考內容:
https://www.tecmint.com/increase-set-open-file-limits-in-linux/
https://www.cnblogs.com/CoolMark-blog/p/12318850.html
相關命令:
#系統當前open file總數(參考)-Ki命令排除線程,包含網絡連接 lsof -Ki|wc -l # 通過系統快照查看 cat /proc/sys/fs/file-nr #以上兩個可能不同,因為下邊的是某一個時刻的快照,系統的open file可能隨時在波動 #以下命令會統計線程,線程是共享文件描述符的(The file descriptors are shared between the threads.) lsof|wc -l #查看某個pid的open files數量 lsof -p 8957 lsof -Ki | grep 8957| wc -l