21

关于linux too many open files的解决方案

 5 years ago
source link: https://www.linuxprobe.com/linux-too-file.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

这个问题的出现原因是system对程序打开files数量的限制问题。

ulimit -a命令可以查看

[root@5 ~]# 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) 4096

max locked memory (kbytes, -l) 32

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) 10240

cpu time (seconds, -t) unlimited

max user processes (-u) 4096

virtual memory (kbytes, -v) unlimited

file locks (-x) unlimited

可以看出,对open files的限制数是1024,我们可以通过修改这个值来增加可以打开的文件数。

******************************************************************

临时修改:

将其改为2048,用 ulimit -n 2048.

当你把open files的值增大到一定程度,你的Too many open files就不会再出现了。

******************************************************************

若要 长久 改变这两个参数值,就要修改/ect/security/limits.conf,在文件中加上两行:

代码如下:

* - nofile 65535

* - nproc 65535

注:这行设置了每个用户的默认打开文件数为2048。 注意"nofile"项有两个可能的限制措施。就是项下的hard和soft。 要使修改过得最大打开文件数生效,必须对这两种限制进行设定。 如果使用"-"字符设定, 则hard和soft设定会同时被设定。

硬限制表明soft限制中所能设定的最大值。 soft限制指的是当前系统生效的设置值。 hard限制值可以被普通用户降低。但是不能增加。 soft限制不能设置的比hard限制更高。 只有root用户才能够增加hard限制值。

当增加文件限制描述,可以简单的把当前值双倍。 例子如下, 如果你要提高默认值1024, 最好提高到2048, 如果还要继续增加, 就需要设置成4096。

最后用ulimit -a再次查看,open files的值,没什么问题的话,就已经改过来了。

******************************************************************

-------------------------------------------------------------

经过以上设置,退出shell再次登录,还是不生效,来点绝的

//etc/profile下,加入一行ulimit -SHn 51200 (注意里面已经有一条关于ulimit的设置)

rc.local下,加入一行 ulimit -SHn 51200 (不知这条有没有必要,其实我还没加)

ulimit -S -c 0 > /dev/null 2>&1

这条语句设置了对软件资源和对core文件大小的设置


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK