注意,这个都是针对当前进程的,即,如果你用c语言编程,基本就会直接和这个打交道。
RLIMIT_NPROC The maximum number of processes (or, more precisely on Linux, threads) that can be created for the real user ID of the calling process. Upon encountering this limit, fork(2) fails with the error EAGAIN.我在redis的源码中,见过相关的api调用。
针对单个用户/用户组的资源限制(修改后,重新登录shell后,启动的进程生效)查看:
vim /etc/security/limits.conf如果要修改,则在上述文件中,增加如下两行,前面的通配符,表示匹配任意用户和group
* soft nofile 65535 * hard nofile 65535这里把任意用户的最大文件数量,改为了65535.
另外,我这里看了下elastic search的官网,因为我记得它就是比较繁琐,需要改这些,
其中有如下一段话:
On Linux systems, persistent limits can be set for a particular user by editing the /etc/security/limits.conf file. To set the maximum number of open files for the elasticsearch user to 65,535, add the following line to the limits.conf file: elasticsearch - nofile 65535 This change will only take effect the next time the elasticsearch user opens a new session.意思是,在linux上,针对某个用户的持久化资源,可以通过设置/etc/security/limits.conf。
比如,要设置elasticsearch用户的最大文件数量为65535,需要增加如下行:
elasticsearch - nofile 65535注意,上面还说了,该change只在下次 elasticsearch 开启一个新session时生效。
上面那个elasticsearch的文档,真心不错,大家可以看看。
所以,这个是shell级别的,修改后,要重新登录shell,该修改文件才生效,同时,要在登录shell后,启动的进程才有用。
简单测试我们修改该文件为:
* soft nofile 6666 * hard nofile 6666然后启动一个进程,监听1235端口:
[root@localhost ~]# nc -l 1235然后在另外一个shell中,查看该进程的资源信息:
[root@localhost ~]# netstat -nltp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:1235 0.0.0.0:* LISTEN 7015/nc tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6632/sshd tcp6 0 0 :::3306 :::* LISTEN 6670/mysqld tcp6 0 0 :::1235 :::* LISTEN 7015/nc tcp6 0 0 :::22 :::* LISTEN 6632/sshd [root@localhost ~]# cat /proc/7015/limits Limit Soft Limit Hard Limit Units Max cpu time unlimited unlimited seconds Max file size unlimited unlimited bytes Max data size unlimited unlimited bytes Max stack size 8388608 unlimited bytes Max core file size 0 unlimited bytes Max resident set unlimited unlimited bytes Max processes 3795 3795 processes Max open files 65535 65535 files这里的最后一行可以发现,文件数量为65535.
然后我们关闭shell,重新登录,此时,就会去执行新的/etc/security/limits.conf,设置为6666,然后我们启动个进程:
[root@localhost ~]# nc -l 1236另一个shell中查看该进程:
[root@localhost ~]# netstat -nltp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:1236 0.0.0.0:* LISTEN 7089/nc tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6632/sshd tcp6 0 0 :::3306 :::* LISTEN 6670/mysqld tcp6 0 0 :::1236 :::* LISTEN 7089/nc tcp6 0 0 :::22 :::* LISTEN 6632/sshd [root@localhost ~]# cat /proc/7089/limits Limit Soft Limit Hard Limit Units Max cpu time unlimited unlimited seconds Max file size unlimited unlimited bytes Max data size unlimited unlimited bytes Max stack size 8388608 unlimited bytes Max core file size 0 unlimited bytes Max resident set unlimited unlimited bytes Max processes 3795 3795 processes Max open files 6666 6666 files可以看到,已经变成6666了。
总结一下,该方式,修改文件后,需要重启shell后生效,且需要是在该shell中启动的进程才生效。
ulimit 方式(不推荐)注意,该种方式,仅当前shell生效,且,仅在修改后,启动的进程才有效。
比如,我们这里在shell1下,修改:
[root@localhost ~]# ulimit -HSn 9999 [root@localhost ~]# nc -l 1234然后启动了一个进程,监听1234端口。
然后我们看看该进程的资源信息:
[root@localhost ~]# netstat -nltp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:1234 0.0.0.0:* LISTEN 6982/nc tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6632/sshd tcp6 0 0 :::3306 :::* LISTEN 6670/mysqld tcp6 0 0 :::1234 :::* LISTEN 6982/nc tcp6 0 0 :::22 :::* LISTEN 6632/sshd [root@localhost ~]# cat /proc/6982/limits Limit Soft Limit Hard Limit Units Max cpu time unlimited unlimited seconds Max file size unlimited unlimited bytes Max data size unlimited unlimited bytes Max stack size 8388608 unlimited bytes Max core file size 0 unlimited bytes Max resident set unlimited unlimited bytes Max processes 3795 3795 processes Max open files 9999 9999 files然后,在我关闭该shell,重新登录shell进来后,执行
[root@localhost ~]# nc -l 1234