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."

soft and hard limit的更多相关文章

  1. Soft Drinking(水)

    A. Soft Drinking time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  2. Codeforces Round #107 (Div. 2)---A. Soft Drinking

    Soft Drinking time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  3. BNU27937——Soft Kitty——————【扩展前缀和】

    Soft Kitty Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class n ...

  4. Process Kill Technology && Process Protection Against In Linux

    目录 . 引言 . Kill Process By Kill Command && SIGNAL . Kill Process By Resource Limits . Kill Pr ...

  5. AIX日常维护

    1  /etc/security/limits与limit命令 AIX 5.3上 下面是文件/etc/security/limits文件里面有关软限制和硬限制的部分. * * Sizes are in ...

  6. Oracle安装基本步骤

    安装数据库 .建立用户组及用户 groupadd oinstall groupadd dba groupadd oper useradd -g oinstall -G dba oracle passw ...

  7. AIX stack_hard参数

    OS:AIX 7.1 root.oracle.grid用户查看资源限制,其中stack受限,需要在/etc/security/limits中增加stack_hard = -1,受硬限制限制! [ora ...

  8. FAQ of db2fmp messages in the db2diag.log

    http://www-01.ibm.com/support/docview.wss?uid=swg21470035 Technote (FAQ) Question What do these mess ...

  9. Redis源代码分析(二十二)--- networking网络协议传输

    上次我仅仅分析了Redis网络部分的代码一部分,今天我把networking的代码实现部分也学习了一遍,netWorking的代码很多其它偏重的是Clientclient的操作.里面addReply( ...

随机推荐

  1. 寻找最佳路径(ArcPy实现)

    一.背景 随着社会经济发展需求,公路的重要性日益提高.在一些交通欠发达的地区,公路建设迫在眉睫.如何根据实际地形情况设计出比较合理的公路规划,是一个值得研究的问题. 二.实验目的: (1)通过练习,熟 ...

  2. vj-2021.6.5-补题

    B - B 内容: One day, as Sherlock Holmes was tracking down one very important criminal, he found a wond ...

  3. Docker 常见命令

    Docker 运行流程 辅助命令 # 1.安装完成辅助命令 docker version -------------------------- 查看docker的信息 docker info ---- ...

  4. FastAPI 学习之路(十四)响应模型

    系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...

  5. 命名空间、作用域、LEGB法则、垃圾回收机制

    一.命名空间.作用域.LEGB法则. 1.命名空间和作用域 : 命名空间:变量名称与值的映射关系 作用域:变量作用的区域,即范围. 注意:class/def/模块会产生作用域:分支语句,循环语句,异常 ...

  6. C++ 类继承 笔记(初步)

    本节内容源于对C++ primer第13章的学习,这本书把C++的原理将得明明白白.网上的博客往往讲得一头雾水.到头来还不如看原书本. 问题 首先给出一题: #include<stdio.h&g ...

  7. 微信h5跳转小程序wx-open-launch-weapp开放标签不显示(已解决)

    前言: 前几天成功对接了跳转第三方小程序的功能,今天有个页面有需要对接.但是奇怪的是用的和上次一模一样的配置,但就是死活不显示wx-open-launch-weapp这个开放标签的按钮,看不到任何效果 ...

  8. js--Symbol 符号基本数据类型

    前言 ECMAScript 6 中新增了 Symbol 符号这一基本数据类型,那么Symbol 是用来干什么的,对开发又有什么帮助呢?本文来总结记录一下 Symbol 的相关知识点. 正文 Symbo ...

  9. 【数据结构与算法Python版学习笔记】树——二叉查找树 Binary Search Tree

    二叉搜索树,它是映射的另一种实现 映射抽象数据类型前面两种实现,它们分别是列表二分搜索和散列表. 操作 Map()新建一个空的映射. put(key, val)往映射中加入一个新的键-值对.如果键已经 ...

  10. 整数划分为k份

    题目 将整数n分成k份,且每份不能为空,任意两个方案不能相同(不考虑顺序). 例如:n=7,k=3,下面三种分法被认为是相同的. 1,1,5; 1,5,1; 5,1,1; 问有多少种不同的分法. 输入 ...