https://www.cnblogs.com/conanwang/p/5818441.html
SU: failed to execute /bin/bash:系統(tǒng)中打開的文件過多
一、產(chǎn)生原因
too many open files(打開的文件過多)是Linux系統(tǒng)中常見的錯(cuò)誤,從字面意思上看就是說程序打開的文件數(shù)過多,不過這里的files不單是文件的意思,也包括打開的通訊鏈接(比如socket),正在監(jiān)聽的端口等等,所以有時(shí)候也可以叫做句柄(handle),這個(gè)錯(cuò)誤通常也可以叫做句柄數(shù)超出系統(tǒng)限制。
引起的原因就是進(jìn)程在某個(gè)時(shí)刻打開了超過系統(tǒng)限制的文件數(shù)量以及通訊鏈接數(shù),通過命令ulimit -a可以查看當(dāng)前系統(tǒng)設(shè)置的最大句柄數(shù)是多少:
[root@localhost ~]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 126906
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024 <==文件數(shù)
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 126906
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
open files那一行就代表系統(tǒng)目前允許單個(gè)進(jìn)程打開的最大句柄數(shù),這里是1024。
使用命令lsof -p 進(jìn)程id可以查看單個(gè)進(jìn)程所有打開的文件詳情,使用命令lsof -p 進(jìn)程id | wc -l可以統(tǒng)計(jì)進(jìn)程打開了多少文件:
[tomcat@localhost bin]$ jps
3092 Bootstrap
3197 Jps
[tomcat@localhost bin]$ lsof -p 3092 | wc -l
108
以裸啟動(dòng)的tomcat為例,可以看到它目前打開了108個(gè)文件數(shù),如果文件數(shù)過多使用lsof -p 進(jìn)程id命令無法完全查看的話,可以使用lsof -p 進(jìn)程id > openfiles.log將執(zhí)行結(jié)果內(nèi)容輸出到日志文件中查看。
二、解決方法
1、增大允許打開的文件數(shù)——命令方式
ulimit -HSn 102400 //這只是在當(dāng)前終端有效,退出之后,open files又變?yōu)槟J(rèn)值。當(dāng)然也可以寫到/etc/profile中,因?yàn)槊看蔚卿浗K端時(shí),都會(huì)自動(dòng)執(zhí)行/etc/profile
ulimit -HSn 102400 臨時(shí)終端生效,切換終端失敗
[root@localhost 20221108]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 126906
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 126906
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
[root@localhost 20221108]#
[root@localhost 20221108]# ulimit -HSn 102400
[root@localhost 20221108]#
[root@localhost 20221108]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 126906
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 102400
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 126906
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
2、增大允許打開的文件數(shù)——修改系統(tǒng)配置文件
2.1.修改file-max
/proc/sys/fs/file-max中指定了系統(tǒng)范圍內(nèi)所有進(jìn)程可打開的文件句柄的數(shù)量限制
# echo 6553560 > /proc/sys/fs/file-max //sysctl -w "fs.file-max=34166",前面2種重啟機(jī)器后會(huì)恢復(fù)為默認(rèn)值
或
# vim /etc/sysctl.conf, 加入以下內(nèi)容,重啟生效
fs.file-max = 6553560
fs.file-max = 6815744
fs.file-max指系統(tǒng)能夠打開最大的文件句柄數(shù)
建議設(shè)置:fs.file-max=512processes=6.51024*1024=6.5MOracle系統(tǒng)內(nèi)核參數(shù)、資源限制及ipcs相關(guān)命令總結(jié)
2.2.修改ulimit的open file,系統(tǒng)默認(rèn)的ulimit對(duì)文件打開數(shù)量的限制是1024
# vim /etc/security/limits.conf //加入以下配置,重啟即可生效
#在最后加入
* soft nofile 65535
* hard nofile 65535
或者只加入
* - nofile 8192
最前的 * 表示所有用戶,可根據(jù)需要設(shè)置某一用戶,例如
roy soft nofile 8192
roy hard nofile 8192
注意”nofile”項(xiàng)有兩個(gè)可能的限制措施。就是項(xiàng)下的hard和soft。 要使修改過得最大打開文件數(shù)生效,必須對(duì)這兩種限制進(jìn)行設(shè)定。 如果使用”-“字符設(shè)定, 則hard和soft設(shè)定會(huì)同時(shí)被設(shè)定。
3、檢查程序問題
如果你對(duì)你的程序有一定的解的話,應(yīng)該對(duì)程序打開文件數(shù)(鏈接數(shù))上限有一定的估算,如果感覺數(shù)字異常,請(qǐng)使用第一步的lsof -p 進(jìn)程id > openfiles.log命令,獲得當(dāng)前占用句柄的全部詳情進(jìn)行分析,
1)打開的這些文件是不是都是必要的?
2)定位到打開這些文件的代碼
3)是否程序操作了文件寫入,但是沒有進(jìn)行正常關(guān)閉
4)是否程序進(jìn)行了通訊,但是沒有正常關(guān)閉(也就是沒有超時(shí)結(jié)束的機(jī)制)
如果程序中存在這些問題的話,無論系統(tǒng)句柄數(shù)設(shè)置的多么大,隨著時(shí)間的推移,也一定會(huì)占用完。
附錄:
附錄1.
為了讓一個(gè)程序的open files數(shù)目擴(kuò)大,可以在啟動(dòng)腳本前面加上ulimit -HSn 102400命令。但當(dāng)程序是一個(gè)daemon時(shí),可能這種方法無效,因?yàn)闆]有終端。文章來源:http://www.zghlxwxcb.cn/news/detail-596802.html
附錄2.
如果某項(xiàng)服務(wù)已經(jīng)啟動(dòng),再動(dòng)態(tài)調(diào)整ulimit是無效的,特別是涉及到線上業(yè)務(wù)就更麻煩了。
這時(shí),可以考慮通過修改/proc/’程序pid’/limits來實(shí)現(xiàn)動(dòng)態(tài)修改?。。?span toymoban-style="hidden">文章來源地址http://www.zghlxwxcb.cn/news/detail-596802.html
到了這里,關(guān)于too many open files(打開的文件過多)解決方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!