(測試系統Centos7)centos
測試腳本bash
[root@up01 ~]# cat /tmp/test_ulimit.py import sys def test_n(): total=[] try: for i in range(0,100000): total.append(open('/tmp/{}-{}'.format('jjkkll',i),'w')) except Exception as err: print(i, repr(err)) test_n()
關於/etc/security/limits.conf中開篇既有兩句話提示app
#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(經試驗亦無效) 中的命令無效。ide
另外經驗證:同時在/etc/security/limits.conf和/etc/profile中設置不一樣的值,/etc/profile中的值最終生效(說明PAM認證在profile以前)測試
首相嘗試了在/etc/profile
中添加對應設置,經測試對開機自啓的系統服務
和 /etc/init.d/rc.local
仍然無效(而且驗證了系統服務的運行時間要早於/etc/init.d/rc.local)centos7
[root@centos7-1 ~]# cat /tmp/log-init (1021, "IOError(24, 'Too many open files')") Sat Nov 3 06:35:19 EDT 2018 [root@centos7-1 ~]# cat /tmp/log-rc.local (1021, "IOError(24, 'Too many open files')") Sat Nov 3 06:35:25 EDT 2018 [root@centos7-1 ~]# ulimit -n 3000 [root@centos7-1 ~]#
並且在/etc/profile中假如ulimit配置(以及任何須要root權限運行的命令),那在以其餘用戶登錄時,就會報一條「無權限」類錯誤。是由於 /etc/profile 在系統啓動過程當中執行的時機:(倒數第一或 第二步)即任何用戶登錄前才/先執行/etc/profile,而後再是 ~/.bash_profile(如有的話)code
後經查明,/etc/init.d/xxxx 以及/etc/rc.loca中的命令都是centos7以前的產物,centos7以後系統引導及服務管理由sysV改成了systemd,而systemd對於ulimit中指定的各種限制是在xxxx.service文件中設置的,這也是推薦的方式。例如 LimitNOFILE=65536。orm
/etc/init.d/functions
中增長一句ulimit -n 65536
(以及其餘設置)來解決通過實驗看下來,發現了一個特色,一個進程能打開的文件描述符老是比設置的nofile值小3,這是由於還有0,1,2 三個標準輸入,輸出,錯誤的存在。進程