Total Pageviews

Saturday, 25 June 2016

Ulimit的用法

解决too many open file的有关问题

ulimit是bash的内建命令,并不是一个程序。
ulimit -a 查看所有设置

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

如果单个进程打开的文件句柄数量超过了系统定义的值,就会提到“too many files open”的错误提示。如何知道当前进程打开了多少个文件句柄呢?下面一段小脚本可以帮你查看:


lsof -n |awk ‘{print $2}’|sort|uniq -c |sort -nr|more
其中第一行是打开的文件句柄数量,第二行是进程号。

Linux有硬性限制和软性限制。可以通过ulimit来设定这两个参数
ulimit -HSn 4096
H指定了硬性大小,S指定了软性大小,n表示设定单个进程最大的打开文件句柄数量.

limits.conf

此修改只是临时有效,要想长期有效需要修改:/etc/security/limits.conf
在文件末尾加上:

* soft nofile 65536
* hard nofile 65536
此文件还有其他的相关设置:
以下是文件描述:

#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

ubuntu还需要修改pam

对于ubuntu还需要修改pam.d配置,才能生效

1. sudo vi /etc/pam.d/common-session
2. Add session required pam_limits.so to the end of the file.
3. Reboot the OS.