Linux   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了再谈Linux的ulimit大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

(测试系统Centos7) 测试脚本 [[email protected] ~]# cat /tmp/test_ulimit.py import sys def test_n(): @R_318_10586@l=[] try: for i in range(0,100000): @R_318_10586@l.append(open(‘/tmp/{}-{}‘.format(‘
(测试系统Centos7)

测试脚本

[[email protected] ~]# cat /tmp/test_ulimit.py 
import sys

def test_n():
    @R_318_10586@l=[]
    try:
        for i in range(0,100000):
            @R_318_10586@l.append(open(‘/tmp/{}-{}‘.format(‘jjkkll‘,i),‘w‘))
    except Exception as err:
        print(i,repr(err))

test_n()

关于/etc/security/limit.conf中开篇既有两句话提示

#This file sets the resource limits for the users logged in via PAm.
#It does not affect resource limits of the system services.

意思就是这里的限制只对经过PAM验证的登陆用户有效,并明确说明对开机自启的系统服务(经chkconfig 或systemd控制的开机启动服务)以及 /etc/init.d/rc.local(经试验亦无效) 中的命令无效。

另外经验证:同时在/etc/security/limit.conf和/etc/profile中设置不同的值,/etc/profile中的值最终生效(说明PAM认证在profile之前)

那如何在开启及启动的服务中突破系统认的ulimit值呢?

首相尝试了在/etc/profile添加对应设置,经测试对开机自启的系统服务/etc/init.d/rc.local仍然无效(并且验证了系统服务的运行时间要早于/etc/init.d/rc.local)

[[email protected] ~]# cat /tmp/log-init 
  (1021,"IOError(24,‘Too many open files‘)")
  Sat Nov  3 06:35:19 EDT 2018
[[email protected] ~]# cat /tmp/log-rc.local 
  (1021,‘Too many open files‘)")
  Sat Nov  3 06:35:25 EDT 2018
[[email protected] ~]# ulimit -n
  3000 
[[email protected] ~]#

而且在/etc/profile中假如ulimit配置(以及任何需要root权限运行的命令),那在以其他用户登陆时,就会报一条“无权限”类错误是因为 /etc/profile 在系统启动过程中执行的时机:(倒数第一或 第二步)即任何用户登陆前才/先执行/etc/profile,然后再是 ~/.bash_profile(若有的话)

那么到底该如何解决上面的问题呢?

后经查明,/etc/init.d/xxxx 以及/etc/rc.loca中的命令都是centos7之前的产物,centos7之后系统引导及服务管理由sysV改为了systemd,而systemd对于ulimit中指定的各类限制是在xxxx.service文件中设置的,这也是推荐的方式。例如 LimitNOFILE=65536。

但是如果非要在/etc/init.d/xxxx以及/etc/rc.local中突破认限制的话

  1. 对于/etc/rc.local,有如下方法 @H_502_48@在 /usr/lib/systemd/system/rc-local.service 的 [service] 段下增加相应配置,如 LimitNOFILE=3000
  2. 对于/etc/init.d/xxx @H_502_48@centos7 中还没找到解决方案(其实执着于此也无意义,因为7的服务管理是systemd) @H_502_48@centos6 中科通过在/etc/init.d/functions增加一句ulimit -n 65536(以及其他设置)来解决

经过实验看下来,发现了一个特点,一个进程能打开的文件描述符总是比设置的nofile值小3,这是因为还有0,1,2 三个标准输入,输出错误的存在。

大佬总结

以上是大佬教程为你收集整理的再谈Linux的ulimit全部内容,希望文章能够帮你解决再谈Linux的ulimit所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。