soft限制了资源使用上限; soft可调整; hard限制了soft上限; 普通用户可使用ulimit -H调低hard limit.

限制的是一个进程可用资源, 而不是某个用户总和.

man setrlimit

RLIMIT_MEMLOCK

This is the maximum number of bytes of memory that may be locked into RAM.

RLIMIT_NOFILE

This specifies a value one greater than the maximum file descriptor number that can be opened by this process.

https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html

ulimit provides control over the resources available to processes started by the shell, on systems that allow such control.

临时修改

ulimit命令默认查询/修改soft, 除非明确指定-H. 普通用户不能使用ulimit调大hard, root可以.

$ ulimit -S -n
1024
$ ulimit -H -n
4096
$ ulimit -H -n 2048
$ ulimit -H -n
2048
$ ulimit -S -n
1024
$ ulimit -n 512
$ ulimit -S -n
512
$ ulimit -H -n 4096
-bash: ulimit: open files: cannot modify limit: Operation not permitted
$ ulimit -H -n 3000
-bash: ulimit: open files: cannot modify limit: Operation not permitted

永久修改某个用户的limit

man limits.conf

vi /etc/security/limits.conf:

witness soft nproc 256
witness hard nproc 512
witness soft nofile 256
witness hard nofile 512

重新登录witness用户:

[witness@vm102 ~]$ ulimit -H -n
512
[witness@vm102 ~]$ ulimit -S -n
256
[witness@vm102 ~]$ ulimit -S -u
256
[witness@vm102 ~]$ ulimit -H -u
512

另一种方式: 把ulimit指令放在.bash_profile/.bashrc中[1][2].

proc文件系统可以看到进程当前的limits

$ cat /proc/23/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 1598 1598 processes
Max open files 1024 4096 files
Max locked memory 65536 65536 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 1598 1598 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us

nofile/ulimit -n

碰到这个错误: too many open files, 就是这个值不够, 或程序资源没释放. 如果需要可以调大一点.

给别人留一点: hard limit应该小于/proc/sys/fs/file-max[2][3]:

It is not recommend to set the "hard" limit for nofile for the oracle user equal to /proc/sys/fs/file-max. If you do that and the user uses up all the file handles, then the entire system will run out of file handles. This may prevent users logging in as the system cannot open any PAM modules that are required for the login process. That is why the hard limit should be set to 63536 and not 65536.

/var/log/messages: VFS: file-max limit 131072 reached

由于这个是控制的单个进程的资源使用, 一些场景如多进程+多文件, 即每个进程都要打开那么多文件, 就容易超过file-max, 比如进程数200, 文件数500, 200x500=100000 > 65535. 这时候需要调整file-max.

当前系统使用的fd总数: cat /proc/sys/fs/file-nr

进程使用fd数排序, 结果第二列是pid: lsof -n | awk '{print $2}' | sort -nr | uniq -c | sort -nr | more

core/ulimit -c

如果ulimit -c是unlimited, 但是无法生成或找不到core文件, sysctl -a | grep core_pattern.

参考

[1] https://access.redhat.com/solutions/61334

[2] https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/5/html/tuning_and_optimizing_red_hat_enterprise_linux_for_oracle_9i_and_10g_databases/chap-oracle_9i_and_10g_tuning_guide-setting_shell_limits_for_the_oracle_user

[3] https://serverfault.com/questions/235059/vfs-file-max-limit-1231582-reached, "I believe this to be an NFS server bug."

最新文章

  1. Entity Framework 6 Recipes 2nd Edition(12-8)译 -> 重新获取一个属性的原始值
  2. final关键字(final是最终的)
  3. Android应用中实现系统“分享”接口
  4. 外国类似stackoverflow这样的网站访问慢怎么解决-遁地龙卷风
  5. bootstrap插件学习-bootstrap.carousel.js
  6. windows查看端口占用
  7. linux中清除cache的方法
  8. CodeForces Round #280 (Div.2)
  9. jQuery moblie 配合jQuery 实现移动端下拉刷新
  10. linux下java调用.so动态库方法2: JNA
  11. jquery遍历筛选数组的几种方法和遍历解析json对象
  12. Linux格式化字符串
  13. form单选框
  14. 怎么写自己的CMakeLists.txt
  15. angularJS学习(二)
  16. CAN总线学习系列之二——CAN总线与RS485的比较
  17. mysql ON DUPLICATE KEY UPDATE重复插入时更新
  18. EF webapi json序列化 表间相互引用 无限循环问题解决方案
  19. 个性化 UIAlertController
  20. ConcurrentHashMap并不是绝对线程安全的

热门文章

  1. Fortran学习笔记:01 基本格式与变量声明
  2. Oracle数据泵数据迁移
  3. Jmeter使用问题记录
  4. 题解 [HNOI2007]分裂游戏
  5. python爬虫时,解决编码方式问题的万能钥匙(uicode,utf8,gbk......)
  6. dubbo注册中心占位符无法解析问题(二)
  7. JS最简单的定时累加计数器
  8. 四、Implementation: The Building Blocks 实现:构件
  9. LeetCode:数组专题
  10. Spring Cloud Gateway Route Predicate Factory 的使用