1.问题原因

too many open files是Linux系统中常见的错误,从字面意思上看就是说程序打开的文件数过多,不过这里的files不单是文件的意思,也包括打开的通讯链接(比如socket),正在监听的端口等等,所以有时候也可以叫做句柄(handle),这个错误通常也可以叫做句柄数超出系统限制。

通过命令ulimit -a可以查看当前系统设置的最大句柄数是多少

一般linux系统的默认open files是1024

 

2.解决方法

  too many open files的解决方法可以通过设置系统允许打开的最大文件数量。

  (1)命令行方式

    ulimit -n  count

   比如 ulimit -n 4096  设置可以打开的最大文件数量为4096

   注:这种方法在系统重启后会失效,还原为默认值。

    ulimit -n命令非root用户只能设置到4096。

   想要设置到更大需要sudo权限或者root用户。

 (2) 通过修改系统配置文件的方式

          通过配置文件设置系统重启后仍然有效

vim /etc/security/limits.conf 

#在最后加入 

* soft nofile 4096 

* hard nofile 4096 

#或者只加入

* - nofile 8192

 

limits.conf配置文件可以配置的格式如下(配置文件中有说明):

#<domain>        <type>  <item>  <value>

#

#Where:

#<domain> can be:

#        - a user name

#        - a group name, with @group syntax

#        - the wildcard *, for default entry

#        - the wildcard %, can be also used with %group syntax,

#                 for maxlogin limit

#

#<type> can have the two values:

#        - "soft" for enforcing the soft limits

#        - "hard" for enforcing hard limits

#

#<item> can be one of the following:

#        - core - limits the core file size (KB)

#        - data - max data size (KB)

#        - fsize - maximum filesize (KB)

#        - memlock - max locked-in-memory address space (KB)

#        - nofile - max number of open file descriptors

#        - rss - max resident set size (KB)

#        - stack - max stack size (KB)

#        - cpu - max CPU time (MIN)

#        - nproc - max number of processes

#        - as - address space limit (KB)

#        - maxlogins - max number of logins for this user

#        - maxsyslogins - max number of logins on the system

#        - priority - the priority to run user process with

#        - locks - max number of file locks the user can hold

#        - sigpending - max number of pending signals

#        - msgqueue - max memory used by POSIX message queues (bytes)

#        - nice - max nice priority allowed to raise to values: [-20, 19]

#        - rtprio - max realtime priority

 

格式如下:

<domain> <type> <item> <value>

domain是以@开头的用户名或组名,*表示所有用户

type设置为hard or soft

item指定想限制的资源。如cpu,core nofile nproc or maxlogins

value是相应的限制值

 

如果应用程序是用systemctl启动的话,该程序的最大文件句柄数也收systemctl配置的限制。需要修改以下配置:

     

 

感谢:https://blog.csdn.net/qq_18298439/article/details/83896777

 

 

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐