日期:2014-05-16  浏览次数:20790 次

我使用过的Linux命令之ulimit - 在shell进程中限制系统资源

我使用过的Linux命令之ulimit - 在shell进程中限制系统资源

本文链接:http://codingstandards.iteye.com/blog/967718 ? (转载请注明出处)

用途说明

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设置,如下所示:

vi +/ulimit /etc/profile 写道
# No core files by default
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)