解决too many open file的有关问题

解决too many open file的问题

通过ulimit -n 命令可以查看linux系统里打开文件描述符的最大值,一般缺省值是1024,对一台繁忙的服务器来说,这个值偏小,所以有必要重新设置linux系统里打开文件描述符的最大值。

 

比如说想把linux系统里打开文件描述符的最大值设置为65535, 命令是ulimit -n 65535。需要说明的是,这是一个和会话相关的命令,比如说你打开两个命令行终端,分别设置一个不同的值,再查询,会发现彼此不受影响。

 

那么到底应该在哪里设置linux系统里打开文件描述符的最大值呢?

 

一个最常见的错误就是在/etc/rc.local里设置,因为rc.local是在最后才被加载的,所以前面加载的程序,如/etc/init.d里的程序都没有机会使用到这个设置值,只有rc.local文件里ulimit -n 65535声明后面的命令才能使用到这个设置值,因为它们在同一个会话里。

 

最正确的做法是在/etc/security/limits.conf里设置:

 

* hard nofile 65535
* soft nofile 65535

 

这样设置后所有的程序,所有的会话就都能使用到这个设置值了。

 

以下是文件描述:

 

#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
#        - 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
#        - rtprio - max realtime priority