Linux 打开文件数 too many open files

2019-05-15  本文已影响0人  Jerry_1116

1 问题描述

too many open files

2 原因分析

出现这句提示的原因是程序打开的文件/socket连接数量超过系统设定值。
查看每个用户最大允许打开文件数量命令: ulimit -a

~$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 20
file size               (blocks, -f) unlimited
pending signals                 (-i) 16382
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) unlimited
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

其中 open files (-n) 1024 表示每个用户最大允许打开的文件数量是1024。

3 解决方案

重新设置open files数值。

3.1 设置当前登录有效,重启失效

ulimit -n 2048

~$ ulimit -n 2048
~$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 20
file size               (blocks, -f) unlimited
pending signals                 (-i) 16382
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 2048
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) unlimited
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

这样就可以把当前用户的最大允许打开文件数量设置为2048了,但这种设置方法在重启后会还原为默认值。

3.2 系统配置文件设置,永久生效

永久设置方法

  1. 打开设置文件
    vim /etc/security/limits.conf
  2. 在最后加入
* soft nofile 4096
* hard nofile 4096

最前的 * 表示所有用户,可根据需要设置某一用户,例如

userA soft nofile 8192
userA hard nofile 8192
  1. 改完后注销,重新登录生效。

参考

  1. linux 打开文件数 too many open files 解决方法
上一篇 下一篇

猜你喜欢

热点阅读