码迷,mamicode.com
首页 > 其他好文 > 详细

ulimit详解

时间:2015-10-30 02:20:08      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:ulimit   系统调优   

  • 概述

  ulimit 是bash内键命令,它具有一套参数集,用于为由它生成的 shell 进程及其子进程的资源使用设置限制。可以用help ulimit 查询手册。



  • help手册

[root@web ~]# help ulimit
ulimit: ulimit [-SHacdefilmnpqrstuvx] [limit]
    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
      -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
  • 选项详解

  我们可以通过ulimit -a来查看当前系统所有的限定值。

[root@web ~]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 3701
max locked memory       (kbytes, -l) 64
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) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 3701
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
  • 选项实例


选项
例子
Sulimit -Hs 64 限制硬资源,线程栈大小为64K
Hulimit -Sn 32 限制软资源,32个文件描述符
aulimit -a 显示当前所有limit信息
culimit -c unlimited 对生成的core文件大小不进行限制
dulimit -d unlimited 对生成的数据段大小不进行限制
fulimit -f 2048 限制进程可创建的最大文件大小为2048 blocks
lulimit -l 32 限制最大可加锁内存大小为 kbytes
mulimit -m unlimited 对最大内存不进行限制
nulimit -n 128 限制最大可使用128个文件描述符
pulimit -p 512 限制管道缓冲区的大小为512 kbytes
sulimit -s 512 限制线程栈的大小为512 kbytes
tulimit -t unlimited 对最大的cpu占用时间不进行限制
uulimit -u 64 限制用户最多可以使用64个进程
vulimit -v 200000 限制最大可用的虚拟内存为 200000 kbytes 




  • ulimit的使用方式

  1. 在用户的启动脚本里

     如果用户使用的是 bash,就可以在用户的目录下的 .bashrc 文件中,加入 ulimit – u 64,来限制用户最多可以使用 64 个进程。此外,可以在与 .bashrc 功能相当的启动脚本中加入 ulimt。

2.  应用程序的启动脚本中

  编写个简单的启动脚本,startmyapp

#/bin/sh
ulimit -n 512
myapp.sh

以这个startmyapp启动myapp.sh时,myapp.sh这个脚本打开的文件句柄数不超过512个。

3. 直接控制台输入

[root@web ~]# ulimit -p 256

限制管道缓冲区为256k




  • ulimit使用范围

  ulimit 作为对资源使用限制的一种工作,是有其作用范围的。那么,它限制的对象是单个用户,单个进程,还是整个系统呢?事实上,ulimit 限制的是当前 shell 进程以及其派生的子进程。

  举例来说,如果用户同时运行了两个 shell 终端进程,只在其中一个环境中执行了 ulimit – s 100,则该 shell 进程里创建文件的大小收到相应的限制,而同时另一个 shell 终端包括其上运行的子程序都不会受其影响。




  • 相关配置文件

  通过修改系统的 /etc/security/limits 配置文件。该文件不仅能限制指定用户的资源使用,还能限制指定组的资源使用。该文件的每一行都是对限定的一个描述,格式如下:

<domain>      <type>  <item>         <value>
#

#*               soft    core            0
*               hard    nofile          100
名称含义
domain表示用户或者组的名字,还可以使用 * 作为通配符。
type可以有两个值,soft 和 hard。
item则表示需要限定的资源,可以有很多候选值,如 stack,cpu,nofile 等等,分别表示最大的堆栈大小,占用的 cpu 时间,以及打开的文件数。
value对应相应的值

上述配置语句限定了任意用户所能创建的最大文件数是 100。

注意

很多应用需要对整个系统的资源使用做一个总的限制,这时候我们需要修改 /proc 下的配置文件。





ulimit详解

标签:ulimit   系统调优   

原文地址:http://chboy.blog.51cto.com/9959876/1707848

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!