#Bash 的 ulimit 命令
ulimit [OPTION] MODE
功能
查看或修改 shell 的资源限制。
内存大小的限制通常以
KiB为单位;文件大小的限制通常以块为单位。
类型
Bash 内置命令。
参数
OPTION选项:-S- 使用“软”资源限制-H- 使用“硬”资源限制-a- 报告所有资源限制-b- 套接字(Socket)的缓冲区大小-c- 创建的核心转储文件的最大大小;单位是块(通常一个块是 512 字节)-d- 进程数据段的最大大小;单位是 KiB-e- 最高调度优先级(nice)-f- 写入文件的最大大小;单位是块(通常一个块是 512 字节)-i- 待处理信号的最大数量-k- 为该进程分配的最大 K 队列(kqueue)数-l- 最大锁定内存(mlock)大小;单位是 KiB-m- 最大驻留内存(RES)大小;单位是 KiB,通常被忽略-n- 文件描述符的最大数量-p- 队列(PIPE)的缓冲区大小-q- POSIX 消息队列中的最大大小;单位是字节-r- 最大实时调度优先级(chrt)-s- 栈的最大尺寸;单位是 KiB-t- 最大 CPU 时间;单位是秒-u- 用户进程的最大数量-v- 虚拟内存(VIRT)的大小;单位是字节-x- 文件锁的最大数量-P- 伪末端的最大数量-R- 实时进程在阻塞之前可以运行的最长时间-T- 线程数量的最大值
MODE- 掩码;可以表示权限的字符串或八进制数值
#示例
查看资源限制
$ ulimit -a
real-time non-blocking time (microseconds, -R) unlimited
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 55331
max locked memory (kbytes, -l) 1781052
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) 55331
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
修改资源限制
$ ulimit -n unlimited # 将文件描述符数量设为无限
$ ulimit -s 10240 # 将栈大小设为 10MiB
#相关命令
| 命令 | 说明 |
|---|---|
| prlimit | 设置单个进程的资源限制 |
#推荐阅读
#手册
ulimit: ulimit [-SHabcdefiklmnpqrstuvxPRT] [limit]
Modify shell resource limits.
Provides control over the resources available to the shell and processes
it creates, on systems that allow such control.
Options:
-S use the `soft' resource limit
-H use the `hard' resource limit
-a all current limits are reported
-b the socket buffer size
-c the maximum size of core files created
-d the maximum size of a process's data segment
-e the maximum scheduling priority (`nice')
-f the maximum size of files written by the shell and its children
-i the maximum number of pending signals
-k the maximum number of kqueues allocated for this process
-l the maximum size a process may lock into memory
-m the maximum resident set size
-n the maximum number of open file descriptors
-p the pipe buffer size
-q the maximum number of bytes in POSIX message queues
-r the maximum real-time scheduling priority
-s the maximum stack size
-t the maximum amount of cpu time in seconds
-u the maximum number of user processes
-v the size of virtual memory
-x the maximum number of file locks
-P the maximum number of pseudoterminals
-R the maximum time a real-time process can run before blocking
-T the maximum number of threads
Not all options are available on all platforms.
If LIMIT is given, it is the new value of the specified resource; the
special LIMIT values `soft', `hard', and `unlimited' stand for the
current soft limit, the current hard limit, and no limit, respectively.
Otherwise, the current value of the specified resource is printed. If
no option is given, then -f is assumed.
Values are in 1024-byte increments, except for -t, which is in seconds,
-p, which is in increments of 512 bytes, and -u, which is an unscaled
number of processes.
Exit Status:
Returns success unless an invalid option is supplied or an error occurs.