目录
事件记录
由于今天需要在测试环境把应用从root用户迁移到inmgr用户,当时创建了inmgr用户就直接进行了迁移。5分钟后应用直接出现OOM异常,经过上网查实,新建用户Linux系统会限制用户的最大进程数。应用程序占满进程数过后,执行任何命令都会报:
/bin/bash: Resource temporarily unavailable。
新用户的进程数限制一般都是在1024或者2048。
解决方案
现在以这台服务器为例:
- 使用root用户登陆,进入到
/etc/security/limits.d/
:[root@VM_3_220_centos ~]$ cd /etc/security/limits.d/ [root@VM_3_220_centos limits.d]$ ll total 8 -rw-r--r-- 1 root root 195 Aug 29 2017 20-nproc.conf -rw-r--r-- 1 root root 60 Apr 21 2016 80-nofile.conf
-
编辑20-nproc.conf:
[root@VM_3_220_centos limits.d]$ vim 20-nproc.conf
-
添加以下信息:
inmgr soft nproc 60000
添加后的文件内容应该为:
# Default limit for number of user's processes to prevent # accidental fork bombs. # See rhbz #432903 for reasoning. * soft nproc 4096 root soft nproc unlimited inmgr soft nproc 60000
-
编辑
/etc/security/limits.conf
,添加以下内容:inmgr soft nproc 60000 inmgr hard nproc 65535 inmgr soft nofile 60000 inmgr hard bofile 65535
编辑后的文件内容为:
# End of file * soft nofile 100001 * hard nofile 100002 root soft nofile 100001 root hard nofile 100002 inmgr soft nproc 65535 inmgr hard nproc 65535 inmgr soft nofile 60000 inmgr hard nofile 65535
nproc:表示max number of processes
nofile:表示max number of open file descriptors
hard/soft:soft是一个警告值,而hard则是一个真正意义的阀值,超过就会报错。 -
再次切换到inmgr用户,执行
ulimit -a
:[inmgr@VM_3_220_centos ~]$ 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) 7271 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) 65535 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
可以看到最大进程数已经修改为65535了。重启应用后没有出现OOM异常。