今天新搭建了1台jenkins,发布前端代码时报错:
Error: EMFILE: too many open files
在这里插入图片描述

百度了一下,说是系统设置的最大打开文件数太小,修改了系统最大打开文件数,仍然报错。 ssh进服务器手动执行jenkins脚本却成功了,想到jenkins是一个进程(服务),通过进程去执行,是否该进程最大支持的文件数太小,查看发现只有4096,修改成65535后不再报错。

以下是相关命令
查看系统最大打开文件数

[root@ruhr-gitlab ~]# ulimit -n
65535

查看全部限制

[root@ruhr-gitlab ~]# 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) 127020
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 65535
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) unlimited
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
[root@ruhr-gitlab ~]# 

临时修改数量

[root@ruhr-gitlab ~]# ulimit -n 655350
[root@ruhr-gitlab ~]# ulimit -n
655350

永久修改数量(需重启服务器)

[root@ruhr-gitlab ~]# vim /etc/security/limits.conf
#添加以下几行
* soft nproc unlimited
* hard nproc unlimited
* soft nofile 655350
* hard nofile 655350

可以临时和永久都修改,只要服务器不宕机或重启,会一直生效,万一宕机或重启以后,因为配置了/etc/security/limits.conf,所以也没关系。

查看单个进程打开的文件数量上限

  • 先找到进程PID
    以jenkins为例,PID是19369
[root@ruhr-gitlab ~]# ps -ef | grep jenkins
root      19369     1 16 13:49 pts/2    00:03:04 java -jar jenkins.war --httpPort=10010
root      9556  9261  0 14:09 pts/4    00:00:00 grep --color=auto jenkins
[root@ruhr-gitlab ~]# 
  • 查看进程能打开的文件数量上限
[root@ruhr-gitlab ~]# cat /proc/19369/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             127020               127020               processes 
Max open files            4096                 4096                 files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       127020               127020               signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us        
[root@ruhr-gitlab ]#

可以看到max_open_files是4096

修改单个进程打开的文件数量上限
修改/etc/security/limits.conf/etc/security/limits.d/20-nproc.conf,完了重启jenkins服务即可

[root@ruhr-gitlab ]# vim /etc/security/limits.d/20-nproc.conf
*          soft    nproc     65535
root       soft    nproc     unlimited

验证
这里实际生效的/etc/security/limits.conf的配置。
它和/etc/security/limits.d/20-nproc.conf的区别我还不太清楚,等弄清楚了再补充进来

[root@ruhr-gitlab ~]# cat /proc/9136/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             unlimited            unlimited            processes 
Max open files            655350               655350               files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       127020               127020               signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us        
[root@ruhr-gitlab ~]# 
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐