Linux 進程打開最大文件連接數Too many open files


首先出現這個提示的原因:應用程序打開的文件數量超過了系統設定值。

如何查看當前系統每個用戶最大允許打開文件數量:

[root@registry ~]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 3820
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 3820
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

或者

[root@registry ~]# ulimit -n 
1024  

其中 open files (-n) 1024 表示每個用戶最大允許打開的文件數量是1024

#查看一下當前已有的連接數,以此來判斷open files 為1024 是否足夠

[root@registry ~]# lsof -n  |awk '{print$2}'|sort |uniq -c |sort -nr |more
    590 5458
    445 1065
    306 638
    267 770
    190 5682
    189 5457
    158 704
    130 1194
    117 681
     81 1263
     68 5419
     66 640
     62 1
     60 5677
     57 678
     55 1067
     51 9036
     51 1196
     48 5698
     44 677
     44 470
     38 494
     32 637
     29 4430
     20 4614
     17 9250
     14 4566
     12 9256
     12 9251
     11 9254
     11 9252
     10 9255
     10 693
      9 9253
      3 92
      3 9037
      3 9033
      3 9
      3 8964
      3 8
      3 726

其中第一列是打開的文件句柄數量,第二列是進程id號

通過句柄數量最多的進程,查看是哪個應用程序打開了最多的文件句柄,通過命令:

[root@registry ~]# ps -aef |grep PID

設置open files 數值的方法

1.臨時設置方法

[root@registry ~]# ulimit -n 4096
[root@registry ~]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 3820
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 4096
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 3820
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

這樣就可以把當前用戶的最大允許打開文件數量設置為4096,但這種設置方法在重啟后會還原為默認值。

2.永久設置方法

[root@registry ~]# vim /etc/security/limits.conf
# End of file
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535

在文件末尾添加上述四行,其中后面兩行的 * 表示所有的用戶,也可以根據需要設置某一個用戶,比如www

# End of file
root soft nofile 65535
root hard nofile 65535
www  soft nofile 65535
www  hard nofile 65535

注銷賬戶,重新登錄即可生效。 

如果想查看某個進程最大允許打開的文件數量,可通過如下命令查看:

[root@registry ~]# top 
top - 15:39:21 up 101 days,  6:26,  1 user,  load average: 0.33, 0.57, 0.52
Tasks:  70 total,   1 running,  69 sleeping,   0 stopped,   0 zombie
%Cpu(s):  6.7 us,  0.0 sy,  0.0 ni, 93.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  3881824 total,   111956 free,  3505012 used,   264856 buff/cache
KiB Swap:  2047996 total,  1001156 free,  1046840 used.   123968 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                                                                 
12872 www       20   0 2738092 743948   2292 S  6.7 19.2 652:05.43 java                                                                                                                    
    1 root      20   0   43380   2244   1352 S  0.0  0.1   4:11.26 systemd                                                                                                                 
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.31 kthreadd                                                                                                                
    3 root      20   0       0      0      0 S  0.0  0.0  28:18.97 ksoftirqd/0                                                                                                             
    5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H                                                                                                            
    7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0                                                                                                             
    8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh                                                                                                                  
    9 root      20   0       0      0      0 S  0.0  0.0  33:52.08 rcu_sched                                                                                                               
   10 root      rt   0       0      0      0 S  0.0  0.0   0:39.61 watchdog/0                                                                                                              
   12 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs                                                                                                               
   13 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 netns                                                                                                                   
   14 root      20   0       0      0      0 S  0.0  0.0   0:07.72 khungtaskd                                                                                                              
   15 root       0 -20       0      0      0 S  0.0  0.0   0:00.18 writeback                                                                                                               
   16 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kintegrityd                                                                                                             
   17 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset                                                                                                                  
   18 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kblockd                                                                                                                 
   19 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 md                                                                                                                      
   25 root      20   0       0      0      0 S  0.0  0.0  12:21.57 kswapd0                                                                                                                 
   26 root      25   5       0      0      0 S  0.0  0.0   0:00.00 ksmd                                                                                                                    
   27 root      39  19       0      0      0 S  0.0  0.0   0:17.32 khugepaged                                                                                                              
   28 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 crypto                                                                                                                  
   36 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kthrotld                                                                                                                
   38 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kmpath_rdacd                                                                                                            
   39 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kpsmoused                                                                                                               
   40 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 ipv6_addrconf                                                                                                           
   59 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 deferwq                                                                                                                 
  101 root      20   0       0      0      0 S  0.0  0.0   0:03.48 kauditd                                                                                                                 
  225 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 ata_sff                                                                                                                 
  233 root      20   0       0      0      0 S  0.0  0.0   0:00.00 scsi_eh_0                                                                                                               
  234 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 scsi_tmf_0                                                                                                              
  235 root      20   0       0      0      0 S  0.0  0.0   0:00.00 scsi_eh_1                                                                                                               
  236 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 scsi_tmf_1                                                                                                              
  240 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 ttm_swap                                                                                                                
  252 root       0 -20       0      0      0 S  0.0  0.0   1:07.64 kworker/0:1H                                                                                                            
  257 root      20   0       0      0      0 S  0.0  0.0   3:12.26 jbd2/vda1-8                                                                                                             
  258 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 ext4-rsv-conver                                                                                                         
  325 root      20   0  119080   5460   5316 S  0.0  0.1   5:27.58 systemd-journal                                                                                                         
  354 root      20   0   44032      8      4 S  0.0  0.0   0:00.08 systemd-udevd                                                                                                           
[root@registry ~]# cat /proc/12872/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             65535                65535                processes 
Max open files            65535                65535                files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       15089                15089                signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM