修改程序ulimit限制(不重启应用)

由于线上应用特殊,不能随意重启,找到2种动态修改程序ulimits限制的方法。下面举例修改nginx的core file大小限制

方法一:prlimit工具修改

#安装新版本的util-linux,由于util-linux版本需要大于等于2.21以上才支持prlimit命令(如果系统有此命令请忽略安装步骤)。
cd /usr/local/src
wget https://mirrors.edge.kernel.org/pub/linux/utils/util-linux/v2.27/util-linux-2.27.1.tar.gz
tar -zxvf util-linux-2.27.1.tar.gz
cd util-linux-2.27.1
./configure --prefix=/usr/share/doc/util-linux-2.27
make && make install
cd /usr/share/doc/util-linux-2.27/bin && cp prlimit /usr/bin/

#找出nginx进程
root@VM-131-5-ubuntu:/etc/security# ps -ef |grep nginx |grep -v grep
root      6034     1  0 17:16 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     6037  6034  0 17:16 ?        00:00:00 nginx: worker process      

#找到进程的core文件大小限制
root@VM-131-5-ubuntu:/etc/security# cat /proc/6034/limits  |grep core
Max core file size        0                    unlimited            bytes     

#使用prlimit命令修改core文件大小限制
root@VM-131-5-ubuntu:/etc/security# prlimit -p 6034 --core=unlimited: 

#再次查看该nginx进程的core文件大小限制
root@VM-131-5-ubuntu:/etc/security# cat /proc/6034/limits  |grep core
Max core file size        unlimited            unlimited            bytes  
 

方法二:python3+ resource模块

#找出nginx进程
root@VM-131-5-ubuntu:/etc/security# ps -ef |grep nginx |grep -v grep
root     16436     1  0 13:01 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx    16439 16436  0 13:01 ?        00:00:00 nginx: worker process                   

#找到进程的core文件大小限制
root@VM-131-5-ubuntu:/etc/security# cat /proc/16436/limits  |grep core
Max core file size        0                    unlimited            bytes  

#利用python3 resource模块修改core文件大小限制
root@VM-131-5-ubuntu:/etc/security# python3
Python 3.4.3 (default, Nov 12 2018, 22:25:49) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import resource
>>> resource.prlimit(16436,resource.RLIMIT_CORE,(-1,-1))   
(0, -1)
>>> exit()


#再次查看该nginx进程的core文件大小限制
root@VM-131-5-ubuntu:/etc/security# cat /proc/16436/limits |grep core 
Max core file size     unlimited         unlimited       bytes

#调整句柄
resource.RLIMIT_NOFILE

 

 

 

 

/etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536

上一篇:linux文件传输大小限制配置,【Linux】一些文件限制配置


下一篇:如何将将最大打开文件num设置为65535