A B C

B C

C


22.显示并设置进程资源限度:ulimit

系统的可用资源是有限的,如果不限制用户和进程对系统资源的使用,则很容易陷入资源耗尽的地步,而使用ulimit可以控制进程对可用资源的访问。默认情况下Linux系统的各个资源都做了软硬限制,其中硬限制的作用是控制软限制(换言之,软限制不能高于硬限制)。使用ulimit-a可以查看当前系统的软限制(使用命令ulimit-a–H可查看系统的硬限制)。下面是该命令的运行结果,笔者对每行输出都做了注解。


  1. [root@localhost ~]# ulimit -a

  2. #core

  3. 文件大小,单位是block

  4. ,默认为0

  5. core file size (blocks, -c) 0

  6. #

  7. 数据段大小,单位是kbyte

  8. ,默认不做限制

  9. data seg size (kbytes, -d) unlimited

  10. #

  11. 调度优先级,默认为0

  12. scheduling priority (-e) 0

  13. #

  14. 创建文件的大小,单位是block

  15. ,默认不做限制

  16. file size (blocks, -f) unlimited

  17. #

  18. 挂起的信号数量,默认是8192

  19. pending signals (-i) 8192

  20. #

  21. 最大锁定内存的值,单位是kbyte

  22. ,默认是32

  23. max locked memory (kbytes, -l) 32

  24. #

  25. 最大可用的常驻内存值,单位是kbyte

  26. ,默认不做限制

  27. max memory size (kbytes, -m) unlimited

  28. #

  29. 最大打开的文件数,默认是1024

  30. open files (-n) 1024

  31. #

  32. 管道最大缓冲区的值

  33. pipe size (512 bytes, -p) 8

  34. #

  35. 消息队列的最大值,单位是byte

  36. POSIX message queues (bytes, -q) 819200

  37. #

  38. 程序的实时性优先级,默认为0

  39. realtime priority (-r) 0

  40. #

  41. 栈大小,单位是kbyte

  42. stack size (kbytes, -s) 10240

  43. #

  44. 最大cpu

  45. 占用时间,默认不做限制

  46. cpu time (seconds, -t) unlimited

  47. #

  48. 用户最大进程数,默认是8192

  49. max user processes (-u) 8192

  50. #

  51. 最大虚拟内存,单位是kbyte

  52. ,默认不做限制

  53. virtual memory (kbytes, -v) unlimited

  54. #

  55. 文件锁,默认不做限制

  56. file locks (-x) unlimited


每一行中都包含了相应的改变该项设置的参数,以最大可以打开的文件数为例(open files默认是1024),想要增大至4096则按照如下命令设置(可参照此方法调整其他参数)。


#

设置最大打开的文件数

#

该命令会同时设置硬限制和软限制

[root@localhost ~]# ulimit -n 4096

#

使用-S

参数单独设置软限制

[root@localhost ~]# ulimit -S -n 4096

#

使用-H

参数单独设置硬限制

[root@localhost ~]# ulimit -H -n 4096


使用ulimit直接调整参数,只会在当前运行时生效,一旦系统重启,所有调整过的参数就会变回系统默认值。所以建议将所有的改动放在ulimit的系统配置文件中。相关配置方法请参考笔者对相关配置文件的注释。


  1. [root@localhost ~]# cat etcsecurity/limits.conf

  2. # etcsecurity/limits.conf

  3. #

  4. 该文件是ulimit

  5. 的配置文件,任何对系统的ulimit

  6. 的修改都应该写入该文件

  7. #

  8. 请将所有的设置写到该文件的最后

  9. #Each line describes a limit for a user in the form:

  10. #

  11. 配置应该写成下面这行的格式,即每个配置占用1

  12. 行,每行4

  13. #

  14. 每列分别是<domain> <type> <item> <value>

  15. #<domain> <type> <item> <value>

  16. #

  17. #

  18. 其中:

  19. #<domain>

  20. 可以取的值如下:

  21. # -

  22. 一个用户名

  23. # -

  24. 一个组名,组名前面用@

  25. 符号

  26. # -

  27. 通配符*

  28. # -

  29. 通配符%

  30. #Where:

  31. #<domain> can be:

  32. # - an user name

  33. # - a group name, with @group syntax

  34. # - the wildcard *, for default entry

  35. # - the wildcard %, can be also used with %group syntax,

  36. # for maxlogin limit

  37. #

  38. #<type>

  39. 只有以下两个可用值:

  40. # - soft

  41. 用于设置软限制

  42. # - hard

  43. 用于设置硬限制

  44. #<type> can have the two values:

  45. # - "soft" for enforcing the soft limits

  46. # - "hard" for enforcing hard limits

  47. #

  48. #<item>

  49. 的值可以是以下任意一种:

  50. # - core - core

  51. 文件大小的限制 (KB)

  52. # - data -

  53. 最大数据段限制 (KB)

  54. # - fsize -

  55. 最大文件大小 (KB)

  56. # - memlock -

  57. 最大锁定的内存大小 (KB)

  58. # - nofile -

  59. 最大打开文件数

  60. # - rss -

  61. 最大常驻内存值 (KB)

  62. # - stack -

  63. 最大栈空间大小 (KB)

  64. # - cpu -

  65. 最大CPU

  66. 使用时间 (MIN)

  67. # - nproc -

  68. 最大进程数

  69. # - as -

  70. 虚拟地址空间

  71. # - maxlogins -

  72. 某用户的最大登录数

  73. # - maxsyslogins -

  74. 系统用户最大登录数

  75. # - priority -

  76. 用户进程的运行优先级

  77. # - locks

  78. 用户最大可以锁定文件的数量

  79. # - sigpending -

  80. 最大挂起的信号量数

  81. # - msgqueue - POSIX

  82. 信号队列使用的最大内存值 (bytes)

  83. # - nice -

  84. 最大nice

  85. # - rtprio -

  86. 最大实时优先级

  87. #

  88. #<item> can be one of the following:

  89. # - core - limits the core file size (KB)

  90. # - data - max data size (KB)

  91. # - fsize - maximum filesize (KB)

  92. # - memlock - max locked-in-memory address space (KB)

  93. # - nofile - max number of open files

  94. # - rss - max resident set size (KB)

  95. # - stack - max stack size (KB)

  96. # - cpu - max CPU time (MIN)

  97. # - nproc - max number of processes

  98. # - as - address space limit

  99. # - maxlogins - max number of logins for this user

  100. # - maxsyslogins - max number of logins on the system

  101. # - priority - the priority to run user process with

  102. # - locks - max number of file locks the user can hold

  103. # - sigpending - max number of pending signals

  104. # - msgqueue - max memory used by POSIX message queues (bytes)

  105. # - nice - max nice priority allowed to raise to

  106. # - rtprio - max realtime priority

  107. #

  108. #<domain> <type> <item> <value>

  109. #

  110. #

  111. 以下是使用样例,请参照配置

  112. #* soft core 0

  113. #* hard rss 10000

  114. #@student hard nproc 20

  115. #@faculty soft nproc 20

  116. #@faculty hard nproc 50

  117. #ftp hard nproc 0

  118. #@student - maxlogins 4


23.测试表达式:test

该命令用于测试表达式EXPRESSION的值,根据测试结果返回0(测试失败)或1(测试成功)。由于该命令非常强大且在Shell脚本中非常重要,所以将会专门使用一节来讲解。其基本用法如下:


  1. [root@localhost ~]# test EXPRESSION