一,ulimit的用途
1,
ulimit 用於shell啟動進程所占用的資源,可用於修改系統資源限制
2,
使用ulimit -a 可以查看當前系統的所有限制值
使用ulimit -n <可以同時打開的文件數> 設置用戶可以同時打開的最大文件數(max open files)
新裝的linux默認只有1024,當作為並發訪問量大的服務器時,很容易遇到error: too many open files。
error: too many open files
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,查看ulimit所屬的包
[root@centos8 limits.d]# whereis ulimit ulimit: /usr/bin/ulimit [root@centos8 limits.d]# rpm -qf /usr/bin/ulimit bash-4.4.19-10.el8.x86_64
屬於bash這個包,默認已經安裝
三,查看ulimit的版本和幫助
1,查看版本:
ulimit沒有提供顯示版本的參數,
但它屬於bash包,可以查看bash的版本
[root@centos8 limits.d]# bash --version GNU bash,版本 4.4.19(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2016 Free Software Foundation, Inc. 許可證 GPLv3+: GNU GPL 許可證第三版或者更新版本 <http://gnu.org/licenses/gpl.html> 本軟件是自由軟件,您可以自由地更改和重新發布。 在法律許可的情況下特此明示,本軟件不提供任何擔保。
2,查看幫助
[root@centos8 limits.d]# ulimit --help
3,查看手冊
[root@centos8 limits.d]# man ulimit
四,ulimit的使用例子
1,列出當前的所有限制
#-a: 所有當前限制都被報告
[root@centos8 ~]# ulimit -a core file size (blocks, -c) unlimited data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 14898 max locked memory (kbytes, -l) 16384 max memory size (kbytes, -m) unlimited open files (-n) 1024 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) 14898 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
各字段的說明:
core file size -c:設置core文件的最大值.單位:blocks
data seg size -d:設置數據段的最大值.單位:kbytes
scheduling priority -e:
最高的調度優先級 (`nice')
file size -f:設置創建文件的最大值.單位:blocks
pending signals -i :
最多的可以掛起的信號數
max locked memory -l:設置在內存中鎖定進程的最大值.單位:kbytes
max memory size -m:設置可以使用的常駐內存的最大值.單位:kbytes
open files -n:設置內核可以同時打開的文件描述符的最大值.單位:n
pipe size -p:設置管道緩沖區的最大值.單位:kbytes
stack size -s:設置堆棧的最大值.單位:kbytes
cpu time -t:設置CPU使用時間的最大上限.單位:seconds
virtual memory -
v
:設置虛擬內存的最大值.單位:kbytes
max user processes: -u 用戶最多可開啟的程序數目
file locks -x :最大的文件鎖數量
2,查看/設置用戶最多打開的文件描述符數量
#-n: open files
[root@centos8 limits.d]# ulimit -n 1024 [root@centos8 limits.d]# ulimit -n 65535 [root@centos8 limits.d]# ulimit -n 65535
3,查看/設置用戶最多可開啟的進程數目
#-u: max user processes
[root@centos8 limits.d]# ulimit -u 14898 [root@centos8 limits.d]# ulimit -u 65535 [root@centos8 limits.d]# ulimit -u 65535
4,其他參數:
#-S指soft 限制 (只給出警告信息,而不是硬限制)
#-H指hard 限制
[root@centos8 limits.d]# ulimit -SHn 65535
說明:此命令等效 ulimit -n 65535
五,使對最大進程數和最大文件數的修改永久生效
1,使用 ulimit -n 65535 可即時修改,但重啟后就無效了。
要設置limits.conf配置文件才可以永久生效
2,修改資源的配置文件
[root@centos8 ~]$ vi /etc/security/limits.conf
增加內容:
* soft nofile 65535 * hard nofile 65535 * soft nproc 65535 * hard nproc 65535
內容說明:
limits.conf的格式如下:
<domain> <type> <item> <value>
domain: 起作用的范圍,
通配符*表示是針對所有用戶的限制
也可以指定用戶,例如:root
type: 取值: soft,hard 和 -
soft 指的是當前系統生效的設置值(警告)
hard 表明系統中所能設定的最大值(錯誤)
soft 的限制不能比har 限制高,- 表明同時設置了 soft 和 hard 的值。
item:
nofile - 打開的文件描述符的最大數目
nproc - 進程最大數量
編輯完成后,重啟服務器使生效
六, ulimit的配置對於服務並不起作用,為什么?
因為ulimit和limits.conf的配置只針對登錄用戶,
而對systemd管理的服務不起作用,
服務的limit要在service文件中單獨指定
請參考這一篇:
https://www.cnblogs.com/architectforest/p/12794986.html
七,查看centos版本
[root@centos8 ~]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core)