用途说明
ulimit是一个shell内建命令,用于控制由shell启动的进程的可用资源(Provides control over the resources available to the shell and to processes started by it, on systems that allow such control.)。对资源的限制分为两种,一种是硬性限制,一种是软性限制。硬性限制一旦设定就不能增加(A hard limit cannot be increased once it is set),而软性限制可以增加到硬性控制为止(a soft limit may be increased up to the value of the hard limit)。Linux系统可以对多种资源的使用进行限制,比如允许创建的文件数、允许打开的文件数、是否允许生成core等。要注意的是,该设置只对当前shell进程的子进程产生作用,并不会影响其他shell进程。
最初接触到这个命令,是在从事C/C++开发时经常会出现段错误、断言错误之类的,但是Linux操作系统中默认是不生成core文件的,所以无法确切的知道问题出现的位置。因此,通常的做法就是修改/etc/profile中的那个ulimit设置,如下所示:
ulimit -S -c 0 > /dev/null 2>&1
通常把允许生成的core文件的大小改成100M之类的,比如:
ulimit -S -c 100000000 > /dev/null 2>&1
PS: 由于本人理解的错误,-c的数值单位其实是blocks,一般是1K。具体block size是多少,与具体的文件系统有关,大伙可以参考本文后面列出的参考资料。
常用参数
使用示例
示例一 与ulimit有关的帮助信息
[root@node34 root]# type -a ulimit
ulimit is a shell builtin
[root@node34 root]# help ulimit
ulimit: ulimit [-SHacdflmnpstuv] [limit]
Ulimit provides control over the resources available to processes
started by the shell, on systems that allow such control. If an
option is given, it is interpreted as follows:
-S use the `soft' resource limit
-H use the `hard' resource limit
-a all current limits are reported
-c the maximum size of core files created
-d the maximum size of a process's data segment
-f the maximum size of files created by the shell
-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
-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
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.
[root@node34 root]# cat /etc/security/limits.conf
# /etc/security/limits.conf
#
#Each line describes a limit for a user in the form:
#
#<domain> <type> <item> <value>
#
#Where:
#<domain> can be:
# - an user name
# - a group name, with @group syntax
# - the wildcard *, for default entry
#
#<type> can have the two values:
# - "soft" for enforcing the soft limits
# - "hard" for enforcing hard limits
#
#<item> can be one of the following:
# - core - limits the core file size (KB)
# - data - max data size (KB)
# - fsize - maximum filesize (KB)
# - memlock - max locked-in-memory address space (KB)
# - nofile - max number of open files
# - rss - max resident set size (KB)
# - stack - max stack size (KB)
# - cpu - max CPU time (MIN)
# - nproc - max number of processes
# - as - address space limit
# - maxlogins - max number of logins for this user
# - priority - the priority to run user process with
# - locks - max number of file locks the user can hold
#
#<domain> <type> <item> <value>
#
#* soft core 0
#* hard rss 10000
#@student hard nproc 20
#@faculty soft nproc 20
#@faculty hard nproc 50
#ftp hard nproc 0
#@student - maxlogins 4
# End of file
[root@node34 root]#
示例二 打印当前的资源限制
[root@node34 root]# ulimit -H -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) 4
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
stack size (kbytes, -s) unlimited
cpu time (seconds, -t) unlimited
max user processes (-u) 2048
virtual memory (kbytes, -v) unlimited
[root@node34 root]# ulimit -S -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) 4
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 2048
virtual memory (kbytes, -v) unlimited
[root@node34 root]#
示例三 修改允许打开的文件数
[root@node34 root]# ulimit -n
1024
[root@node34 root]# ulimit -n 4096
[root@node34 root]# ulimit -n
4096
[root@node34 root]#