linux和linux系统范围内用户打开的文件数是多少?

linux和linux系统范围内用户打开的文件数是多少?,linux,unix,lsof,Linux,Unix,Lsof,对不起,这个问题有几个层次,但都涉及打开文件的数量 我在我们正在开发的应用程序的应用程序日志中收到一条“打开的文件太多”消息。有人建议我: 查找当前正在使用的系统范围和每个用户打开的文件数 查找系统和用户打开文件的限制 我运行了ulimit-n,它返回1024。我还查看了/etc/limits.conf,该文件中没有任何特殊内容/etc/sysctl.conf也未修改。我将在下面列出这些文件的内容。我还运行了lsof | wc-l,它返回了5000多行(如果我使用正确的话) 因此,我的主要问题是

对不起,这个问题有几个层次,但都涉及打开文件的数量

我在我们正在开发的应用程序的应用程序日志中收到一条“打开的文件太多”消息。有人建议我:

  • 查找当前正在使用的系统范围和每个用户打开的文件数
  • 查找系统和用户打开文件的限制
  • 我运行了
    ulimit-n
    ,它返回1024。我还查看了/etc/limits.conf,该文件中没有任何特殊内容/etc/sysctl.conf也未修改。我将在下面列出这些文件的内容。我还运行了
    lsof | wc-l
    ,它返回了5000多行(如果我使用正确的话)

    因此,我的主要问题是:

  • 如何查找每个用户允许打开的文件数?nofile设置的软限制是否在/etc/limits.conf中找到/定义?由于我没有触摸/etc/limits.conf,默认设置是什么
  • 如何查找系统范围内允许打开的文件数?它是limits.conf中的硬限制吗?如果limits.conf未修改,默认数字是多少
  • ulimit为打开的文件返回的数字是多少?上面写着1024,但当我运行lsof并计算行数时,它超过了5000+,所以有些东西不适合我。我是否应该运行其他CMD或查看其他文件以获得这些限制?提前感谢你的帮助
  • limits.conf的内容

    # /etc/security/limits.conf
    #
    #Each line describes a limit for a user in the form:
    #
    #<domain>        <type>  <item>  <value>
    #
    #Where:
    #<domain> can be:
    #        - an 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 files
    #        - 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>
    #
    
    #*               soft    core            0
    #*               hard    rss             10000
    #@student        hard    nproc           20
    #@faculty        soft    nproc           20
    #@faculty        hard    nproc           50
    #ftp             hard    nproc           0
    #@student        -       maxlogins       4
    
    # End of file
    

    没有每个用户的文件限制。您需要了解的是整个系统和每个进程。每个进程的文件数限制乘以每个用户的进程数限制,理论上可以提供每个用户的文件数限制,但在正常值下,产品将非常大,实际上是无限的

    另外,lsof最初的目的是列出打开的文件,但现在它已经增长并列出了其他内容,如cwd和mmap区域,这也是它输出的行数超过预期的另一个原因

    错误消息“打开的文件太多”与errno value
    EMFILE
    关联,即每个进程的限制,在您的情况下,该限制似乎为1024。如果您可以找到正确的选项,将lsof限制为仅显示单个进程的实际文件描述符,那么您可能会发现其中有1024个,或者非常接近

    现在很少需要手动调整系统范围的文件描述符限制,因为其默认值与内存成比例。如果需要,您可以在
    /proc/sys/fs/file max
    上找到它,并在
    /proc/sys/fs/file nr
    上找到有关当前使用情况的信息。对于
    file max
    ,您的sysctl文件的值为
    4096
    ,但是它被注释掉了,所以您不应该把它当回事


    如果您试图达到系统范围的限制,您将得到errno
    ENFILE
    ,这将转换为错误消息“文件表溢出”或“系统中打开的文件太多”。

    需要考虑的一个问题是:为什么打开的文件会用完?您真的需要一次打开1024个文件,还是应该确保在完成文件后关闭它们。当然,需要打开大量文件是有正当理由的,但不要因为资源泄露而在一段时间后增加限制以耗尽文件。@JonathanLeffler,thx供您评论。我同意,我想找出谁是罪魁祸首并阻止它。我会调查lsof,看看我是否能找到那个过程。啊,好的,谢谢你让我知道发生了什么。所以我们没有达到系统范围内的限制,但是过程限制,这就是我们抱怨的。我可能需要问你,如果我不能弄清楚,我需要使用什么样的lsof选项。
    # Controls IP packet forwarding
    net.ipv4.ip_forward = 0
    
    # Controls source route verification
    net.ipv4.conf.default.rp_filter = 1
    
    # Do not accept source routing
    net.ipv4.conf.default.accept_source_route = 0
    
    # Controls the System Request debugging functionality of the kernel
    kernel.sysrq = 0
    
    # Controls whether core dumps will append the PID to the core filename
    # Useful for debugging multi-threaded applications
    kernel.core_uses_pid = 1
    
    # Controls the use of TCP syncookies
    net.ipv4.tcp_syncookies = 1
    
    # Controls the maximum size of a message, in bytes
    kernel.msgmnb = 65536
    
    # Controls the default maxmimum size of a mesage queue
    kernel.msgmax = 65536
    
    # Controls the maximum shared segment size, in bytes
    kernel.shmmax = 68719476736
    
    # Controls the maximum number of shared memory segments, in pages
    kernel.shmall = 4294967296
    
    # the interval between the last data packet sent and the first keepalive probe
    net.ipv4.tcp_keepalive_time = 600
    
    # the interval between subsequential keepalive probes
    net.ipv4.tcp_keepalive_intvl = 60
    
    # the interval between the last data packet sent and the first keepalive probe
    net.ipv4.tcp_keepalive_time = 600
    
    # the interval between subsequential keepalive probes
    net.ipv4.tcp_keepalive_intvl = 60
    
    # the number of unacknowledged probes to send before considering the connection dead and notifying the application layer
    net.ipv4.tcp_keepalive_probes = 10
    
    # the number of unacknowledged probes to send before considering the connection dead and notifying the application layer
    net.ipv4.tcp_keepalive_probes = 10
    
    # try as hard as possible not to swap, as safely as possible
    vm.swappiness = 1
    fs.aio-max-nr = 1048576
    #fs.file-max = 4096