linux 错误 too many open files 解决方案

小咪咪 2021-08-24 20:39 443阅读 0赞

too many open files

出现这句提示的原因是程序打开的文件/socket连接数量超过系统设定值。

查看每个用户最大允许打开文件数量

  1. fdipzone@ubuntu:~$ ulimit -a
  2. core file size (blocks, -c) 0
  3. data seg size (kbytes, -d) unlimited
  4. scheduling priority (-e) 20
  5. file size (blocks, -f) unlimited
  6. pending signals (-i) 16382
  7. max locked memory (kbytes, -l) 64
  8. max memory size (kbytes, -m) unlimited
  9. open files (-n) 1024
  10. pipe size (512 bytes, -p) 8
  11. POSIX message queues (bytes, -q) 819200
  12. real-time priority (-r) 0
  13. stack size (kbytes, -s) 8192
  14. cpu time (seconds, -t) unlimited
  15. max user processes (-u) unlimited
  16. virtual memory (kbytes, -v) unlimited
  17. file locks (-x) unlimited

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

查看当前系统打开的文件数量

  1. lsof | wc -l
  2. watch "lsof | wc -l"

查看某一进程的打开文件数量

  1. lsof -p pid | wc -l
  2. lsof -p 1234 | wc -l

设置 open files 数值方法

  1. ulimit -n 2048
  2. fdipzone@ubuntu:~$ ulimit -n 2048
  3. fdipzone@ubuntu:~$ ulimit -a
  4. core file size (blocks, -c) 0
  5. data seg size (kbytes, -d) unlimited
  6. scheduling priority (-e) 20
  7. file size (blocks, -f) unlimited
  8. pending signals (-i) 16382
  9. max locked memory (kbytes, -l) 64
  10. max memory size (kbytes, -m) unlimited
  11. open files (-n) 2048
  12. pipe size (512 bytes, -p) 8
  13. POSIX message queues (bytes, -q) 819200
  14. real-time priority (-r) 0
  15. stack size (kbytes, -s) 8192
  16. cpu time (seconds, -t) unlimited
  17. max user processes (-u) unlimited
  18. virtual memory (kbytes, -v) unlimited
  19. file locks (-x) unlimited

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

永久设置方法

  1. vi /etc/security/limits.conf

在最后加入

  1. * soft nofile 4096
  2. * hard nofile 4096

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

  1. fdipzone soft nofile 8192
  2. fdipzone hard nofile 8192

改完后重启生效。

发表评论

表情:
评论列表 (有 0 条评论,443人围观)

还没有评论,来说两句吧...

相关阅读