ulimit详解

 网上的ulimit设置,只是简单说明了如何设置ulimit,方法各类各样让人一头雾水,这里整理一下。web

目录
shell

1、ulimit简介bash

2、ulimit命令使用服务器

3、ulimit使用方式及做用范围app

4、ulimit相关配置文件dom

5、ulimit -n与file-max的关系和区别 socket


1、ulimit简介ide

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

2、ulimit命令使用线程

一、选项详解

[root@BriefServer ~]# help ulimit
ulimit: ulimit [-SHacdefilmnpqrstuvx] [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
      -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

选项     例子

Hulimit -Hs 64 限制资源,线程栈大小为64K

Sulimit -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 

注意:这个当中的硬限制是实际的限制,而软限制,是warnning限制,只会作出warning;其实ulimit命令自己就有分软硬设置,加-H就是硬,加-S就是软

默认显示的是软限制,若是运行ulimit命令修改的时候没有加上的话,就是两个参数一块儿改变。

3、ulimit的使用方式及做用范围

一、在用户的启动脚本里

     若是用户使用的是 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 限制的是当前shell进程以及其派生的子进程。

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

4、ulimit相关配置文件

主配置文件:/etc/security/limits.conf

分段配置文件:/etc/secutity/limits.d/*.conf

      经过修改系统的 /etc/security/limits.conf配置文件。该文件不只能限制指定用户的资源使用,还能限制指定组的资源使用。该文件的每一行都是对限定的一个描述,

格式以下:

#<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

名称             含义

domain     表示用户或者组的名字,还可使用 * 做为通配符表明全部用户

username|@groupname:设置须要被限制的用户名,组名前面加@和用户名区别。

type          能够有两个值,soft 和 hard。type:有 soft,hard 和 -,soft 指的是当前系统生效的设置值。hard 代表系统中所能设定的最大值。soft 的限制不能比har 限制高。用 - 就代表同时设置了 soft 和 hard 的值。

item         则表示须要限定的资源,能够有不少候选值,如 stack,cpu 等等,分别表示最大的堆栈大小,占用的 cpu 时间,noproc 是表明进程数、nofile 是表明文件打开数

value  对应相应的值

编辑保存以后,须要从新登陆一次才能生效

5、ulimit -n与file-max的关系和区别    

查看Linux系統級的最大打開文件數限制,使用以下命令:

[root@www ~]# cat /proc/sys/fs/file-max 
97938

     這代表這台Linux系統最多允許同時打開(即包含全部用戶打開文件數總和)97938個文件,是Linux系統級硬限制,全部用戶級的打開文件數限制都不應超過這個數值。一般這個系統級硬限制是Linux系統在啟動時根據系統硬件資源狀況計算出來的最佳的最大同時打開文件數限制,若是沒有特殊须要,不應該修改此限制,除非想為用戶級打開文件數限制設置超過此限制的值。

這是讓Linux在啟動完成後強行將系統級打開文件數硬限制設置為1024000,修改完後保存此文件。

     file-max是设置 系统全部进程一共能够打开的文件数量 。同时一些程序能够经过setrlimit调用,设置每一个进程的限制。若是获得大量使用完文件句柄的错误信息,是应该增长这个值。

也就是说,这项参数是系统级别的。


echo  6553560 > /proc/sys/fs/file-max

或修改 /etc/sysctl.conf, 加入

fs.file-max = 6553560 

执行以下命令生效:sysctl -p

      另外还有一个,/proc/sys/fs/file-nr,能够看到整个系统目前使用的文件句柄数量。

查找文件句柄问题的时候,还有一个很实用的程序lsof。能够很方便看到某个进程开了那些句柄,也能够看到某个文件/目录被什么进程占用了。

显然,对服务器来讲,file-max, ulimit都须要设置,不然就可能出现文件描述符用尽的问题,为了让机器在重启以后仍然有效,强烈创建做如下配置,以确保file-max, ulimit的值正确无误:

1. 修改/etc/sysctl.conf, 加入

fs.file-max = 6553560

2.系统默认的ulimit对文件打开数量的限制是1024,修改/etc/security/limits.conf并加入如下配置,永久生效

* soft nofile 65535 
* hard nofile 65535


转自:xiexiaojun

http://xiexiaojun.blog.51cto.com/2305291/1730332

相关文章
相关标签/搜索