linux 打开文件数 too many open files 解决方法

来源:互联网 发布:ubuntu配置网关 编辑:程序博客网 时间:2024/05/17 22:12

现象描述:

由于生产系统网络的升级,导致F5负载均衡器无法使用。系统中正常情况下是起的双节点前置(A节点、B节点),所有客户端都访问F5的地址,然后通过F5转到A节点或B节点,由于F5无法起到负载的作用,所以F5把所有请求直接转到A节点上,导致A节点的交易量是平时的二倍,并发量增加导致系统无法创建socket连接,交易无法进行。linux系统默认的open files是1024.


Linux 打开文件数 too many open files 解决方法


too many open files

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


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

ulimit -a

[plain] view plain copy
  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


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

[plain] view plain copy
  1. lsof | wc -l  
  2. watch "lsof | wc -l"  

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

[plain] view plain copy
  1. lsof -p pid | wc -l  
  2. lsof -p 1234 | wc -l  

设置open files数值方法

ulimit -n 2048

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

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


永久设置方法

[plain] view plain copy
  1. vim /etc/security/limits.conf  
  2. 在最后加入  
  3. * soft nofile 4096  
  4. * hard nofile 4096  
最前的 * 表示所有用户,可根据需要设置某一用户,例如

[plain] view plain copy
  1. fdipzone soft nofile 8192  
  2. fdipzone hard nofile 8192  

改完后注销一下就能生效。


参考:http://blog.csdn.net/fdipzone/article/details/34588803

阅读全文
0 0