`

我使用过的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)
#        - 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]#

要永久修改,请参见后面的资料。

 

问题思考

相关资料

【1】developerWorks 中国   通过 ulimit 改善系统性能
http://www.ibm.com/developerworks/cn/linux/l-cn-ulimit/
【2】有意没思的专栏 linux修改ulimit应该注意的
http://blog.csdn.net/noizz/archive/2008/03/13/2177503.aspx
【3】走在路上的你。。。  查看linux blocksize 大小
http://blogold.chinaunix.net/u/11765/showart_235505.html
【4】好好学习,天天向上 解决linux打开文件数1024限制的解决办法
http://www.51testing.com/?uid-13956-action-viewspace-itemid-209988
【5】我家空间  Linux 用户进程可打开文件数 TCP连接数 限制修改
http://www.myhomespace.net/p/2010/11/528

 

返回 我使用过的Linux命令系列总目录

 

0
3
分享到:
评论

相关推荐

    Linux ulimit命令用法详解

    Linux ulimit命令用于控制shell程序的资源。 ulimit为shell内建指令,可用来控制shell执行程序的资源。 语法 ulimit [-aHS][-c ][-d ][-f ][-m ][-n ][-p ][-s ][-t ][-u ][-v ] 参数: -a 显示目前资源限制的设定...

    ulimit命令 控制shell程序的资源

    为提高性能,可以根据设备资源情况,设置各linux用户的最大进程数,我们可以用ulimit来显示当前的各种用户进程限制。 ulimit为shell内建命令,可用来控制shell执行程序的资源。 语法格式:ulimit [参数] 常用参数...

    linux_ulimit的使用

    linux_ulimit的使用 linux_ulimit的使用 linux_ulimit的使用

    并发时-修改Linux系统下的大文件描述符限制

    通常我们通过终端连接到linux系统后执行ulimit -n 命令可以看到本次登录的session其文件描述符的限制,如下:  $ulimit -n  1024  当然可以通过ulimit -SHn 102400 命令来修改该限制,但这个变更只对当前的...

    LINUX与UNIX SHELL编程指南(很全)

    2.1.13 使用exec或ok来执行shell命令 19 2.1.14 find命令的例子 20 2.2 xargs 20 2.3 小结 21 第3章 后台执行命令 22 3.1 cron和crontab 22 3.1.1 crontab的域 22 3.1.2 crontab条目举例 23 3.1.3 crontab命令选项 ...

    Linux命令搜索工具linux-command.zip

    实验楼 - 免费提供了Linux在线环境,不用在自己机子上装系统也可以学习Linux,超方便实用。 鸟哥的linux私房菜 - 非常适合Linux入门初学者看的教程。 Linux公社 - Linux相关的新闻...

    linux的最大进程句柄数设置

    linux的最大进程句柄数设置在Linux下,我们使用ulimit -n命令可以看到单个进程能够打开的最大文件句柄数量(socket连接也算在里面)。系统默认值1024。

    绝版经典《Linux与UNIX Shell编程指南》

    19.6 在shell中使用函数 204 19.7 创建函数文件 204 19.8 定位文件 205 19.9 检查载入函数 205 19.10 执行shell函数 205 19.10.1 删除shell函数 206 19.10.2 编辑shell函数 206 19.10.3 函数举例 207 19.10.4 将函数...

    LINUX与UNIX SHELL编程指南 高清PDF

    本书共分五部分,详细介绍了shell编程技巧,各种UNIX命令及语法,还涉及了UNIX下的文字处理以及少量的系统管理问题。本书内容全面、文字简洁流畅,适合Shell编程人员学习、参考。 目 录 译者序 前言 第一部分 shell...

    Linux与unix shell编程指南

    19.6 在shell中使用函数 204 19.7 创建函数文件 204 19.8 定位文件 205 19.9 检查载入函数 205 19.10 执行shell函数 205 19.10.1 删除shell函数 206 19.10.2 编辑shell函数 206 19.10.3 函数举例 207 19.10.4 将函数...

    Unix 命令全集

    shell 命令 show 命令 showmount 命令 shutacct 命令 shutdown 命令 size 命令 skulker 命令 slattach 命令 sleep 命令 slibclean 命令 sliplogin 命令 slocal 命令 smcaprop 命令 smdefca 命令 smdemon.cleanu 命令...

    Wrk压力测试脚本--对ONU及路由等通信产品

    注意:在Linux终端下使用ulimit -a查看下一个文件并发数,默认是600,需通过 ulimit -n 60000修改后在允许脚本 在ONU或路由上行创建一个 WEB服务器,尽量使局域网内存的用户访问的网址的大小大一点,效果容易出现; ...

    Linux shell编程指南

    本书共分五部分,详细介绍了shell编程技巧,各种UNIX命令及语法,还涉及了UNIX下的文字处理以及少量的系统管理问题。本书内容全面、文字简洁流畅,适合Shell编程人员学习、参考。 目 录 译者序 前言 第一部分 ...

    linux shell 编程教程

    linux shell编程 教程大全 目 录 译者序 前言 第一部分 shell 第1章 文件安全与权限 1 1.1 文件 1 1.2 文件类型 2 1.3 权限 2 1.4 改变权限位 4 1.4.1 符号模式 4 1.4.2 chmod命令举例 5 1.4.3 绝对模式 5 1.4.4 ...

    marmot-cn#readingNotes#设置linux打开文件句柄:proc:sys:fs:file-max和ulimit

    设置Linux打开文件句柄/proc/sys/fs/file-max和ulimit -n的区别表示系统级别的能够打开的文件句柄的数量.是对整个系统的限制,并不是

    Linux与Unix Shell编程指南(PDF格式,共30章)

    本书共分五部分,详细介绍了shell编程技巧,各种UNIX命令及语法,还涉及了UNIX下的文字处理以及少量的系统管理问题。本书内容全面、文字简洁流畅,适合Shell编程人员学习、参考。 目 录 译者序 前言 第一部分 ...

    LINUX与UNIX SHELL编程指南

    2.1.13 使用exec或ok来执行shell命令 19 2.1.14 find命令的例子 20 2.2 xargs 20 2.3 小结 21 第3章 后台执行命令 22 3.1 cron和crontab 22 3.1.1 crontab的域 22 3.1.2 crontab条目举例 23 3.1.3 crontab命令选项 ...

    LINUX与UNIX_Shell编程指南

    详细的介绍了Linux下的编程指南 目 录 译者序 前言 第一部分 shell 第1章 文件安全与权限 1 1.1 文件 1 1.2 文件类型 2 1.3 权限 2 1.4 改变权限位 4 1.4.1 符号模式 4 1.4.2 chmod命令举例 5 1.4.3 绝对...

Global site tag (gtag.js) - Google Analytics