今天遇到一个 too many open file 的问题,这种问题遇到得多了,一般来说都是有句柄泄露导致的,但是,今天遇到的情况还真的是句柄限制太小(1024)导致的,所以当然得设置一下了。
以前都是直接按照经验,敲上命令 ulimit -S xxxx
就收工了,但是,今天有兴致得看了一下 ulimit 这个命令,然后学到了点东西:
[root@liqiang.io]# ulimit -a
-t: cpu time (seconds) unlimited
-f: file size (blocks) unlimited
-d: data seg size (kbytes) unlimited
-s: stack size (kbytes) 1025
-c: core file size (blocks) 0
-m: resident set size (kbytes) unlimited
-u: processes 61757
-n: file descriptors 1025
-l: locked-in-memory size (kbytes) 64
-v: address space (kbytes) unlimited
-x: file locks unlimited
-i: pending signals 61757
-q: bytes in POSIX msg queues 819200
-e: max nice 0
-r: max rt priority 0
-N 15: unlimited
例如这里有好多个 size,查找了一下资料,然后发现 limit 也分 soft limit 和 hard limit。
什么是 soft limit 和 hard limit
- A hard limit is the maximum allowed to a user, set by the superuser/root. This value is set in the file /etc/security/limits.conf. Think of it as an upper bound or ceiling or roof.
- A soft limit is the effective value right now for that user. The user can increase the soft limit on their own in times of needing more resources, but cannot set the soft limit higher than the hard limit.
怎么修改 soft limit
- 配置文件
[root@liqiang.io]#tail -2 /etc/security/limits.conf
liqiang soft nofile 4096
root soft nofile 4096
命令行
[root@liqiang.io]# ulimit -S -n 2048
将 soft limit 设置为 2048
[root@liqiang.io]# ulimit -S -xx xxxx
帮助
命令/文件 | 描述 |
---|---|
ulimit -S -a |
view all soft limits |
ulimit -H -a |
view all hard limits |
ulimit -S [option] [number] |
set a specific soft limit for one variable |
ulimit -H [option] [number] |
set a specific hard limit for one variable |
/etc/security/limits.conf |
file where you can set soft and hard limits per user or for everyone |