CentOS 6.x online change existing running process’s ulimits

1 minute read

背景

前段时间发了一篇关于CentOS 6.x ulimit配置文件变更为/etc/security/limits.d/90-nproc.conf 的文章.

但是如果你的系统中已经存在的进程的nproc是不会被变更过来的. 例如一个数据库服务器.

PostgreSQL所有的backend process都是从postmaster进程fork出来的, 如果postmaster进程的nproc还是老的配置的话, 资源限制依旧存在, 例如.

> psql  
psql: could not fork new process for connection: Resource temporarily unavailable  
  
could not fork new process for connection: Resource temporarily unavailable  

这种情况通过修改/etc/security/limits.d/90-nproc.conf是无法实现的,

* soft    nproc   131072  
* hard    nproc   131072  

但是又不能重启数据库进程怎么办呢?

只要你的内核够新2.6.32+, 那么就可以通过编辑进程的limits来实现.

例如

> ps -ewf|grep postgres  
postgres  5721  5720  0 Feb26 ?        19:11:05 postgres: logger process     

主进程号是5720

# cd /proc/5720  
# cat 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            10485760             unlimited            bytes       
Max core file size        unlimited            unlimited            bytes       
Max resident set          unlimited            unlimited            bytes       
Max processes             1024                 131072               processes   
Max open files            131072               131072               files       
Max locked memory         51200000000          51200000000          bytes       
Max address space         unlimited            unlimited            bytes       
Max file locks            unlimited            unlimited            locks       
Max pending signals       256607               256607               signals     
Max msgqueue size         819200               819200               bytes       
Max nice priority         0                    0                      
Max realtime priority     0                    0                      
Max realtime timeout      unlimited            unlimited            us          

软限制还是1024, 修改方法

# echo -n "Max processes=131072:131072" > limits  
# cat 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            10485760             unlimited            bytes       
Max core file size        unlimited            unlimited            bytes       
Max resident set          unlimited            unlimited            bytes       
Max processes             131072               131072               processes   
Max open files            131072               131072               files       
Max locked memory         51200000000          51200000000          bytes       
Max address space         unlimited            unlimited            bytes       
Max file locks            unlimited            unlimited            locks       
Max pending signals       256607               256607               signals     
Max msgqueue size         819200               819200               bytes       
Max nice priority         0                    0                      
Max realtime priority     0                    0                      
Max realtime timeout      unlimited            unlimited            us          

千万不要直接编辑这个文件.

限制问题解决了, 不会再报资源不足的错误.

> psql  
psql (9.3.3)  
Type "help" for help.  
postgres=>  

参考

1. http://serverfault.com/questions/201207/set-max-file-limit-on-a-running-process

2. http://blogs.kent.ac.uk/unseenit/2013/06/06/changing-ulimit-for-running-processes/

3. http://www.nowo.com/run-time-changes-ulimit/

4. 《CentOS 6.x 优先级 limit change to /etc/security/limits.d/xxx》

Flag Counter

digoal’s 大量PostgreSQL文章入口