预计阅读本页时间:-
ulimit
The ulimit command was originally used to specify the limit on file creation size. But bash's version has options that let you put limits on several different system resources. Table 10-2 lists the options.
Table 10-2. ulimit resource options
广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元
Option
Resource limited
-a
All limits (for printing values only)
-c
Core file size (1 Kb blocks)
-d
Process data segment (Kb)
-f
File size (1 Kb blocks)
-l
Maximum size of a process that can be locked in memory (Kb)[9]
-m
Maximum resident set size
-n
File descriptors
-p
Pipe size (512 byte blocks)
-s
Process stack segment (Kb)
-t
Process CPU time (seconds)
-u
Maximum number of processes available to a user
-v
Virtual memory (Kb)
[9] Not available in versions of bash prior to 2.0.
Each takes a numerical argument that specifies the limit in units shown in the table. You can also give the argument "unlimited" (which may actually mean some physical limit), "hard" and "soft", which refer to the current hard and soft limits (see below), or you can omit the argument, in which case it will print the current limit. ulimit -a prints limits (or "unlimited") of all types.[10] You can specify only one type of resource at a time. If you don't specify any option, -f is assumed.
Some of these options depend on operating system capabilities that don't exist in older UNIX versions. In particular, some older versions have a fixed limit of 20 file descriptors per process (making -n irrelevant), and some don't support virtual memory (making -v irrelevant).
The -d and -s options have to do with dynamic memory allocation, i.e., memory for which a process asks the operating system at runtime. It's not necessary for casual users to limit these, though software developers may want to do so to prevent buggy programs from trying to allocate endless amounts of memory due to infinite loops.
The -v and -m options are similar; -v puts a limit on all uses of memory, and -m limits the amount of physical memory that a process is allowed to use. You don't need these unless your system has severe memory constraints or you want to limit process size to avoid thrashing.
The -u option is another option which is useful if you have system memory constraints or you wish just wish to stop individual users from hogging the system resources.
You may want to specify limits on file size (-f and -c) if you have constraints on disk space. Sometimes users actually mean to create huge files, but more often than not, a huge file is the result of a buggy program that goes into an infinite loop. Software developers who use debuggers like sdb, dbx, and gdb should not limit core file size, because core dumps are necessary for debugging.
The -t option is another possible guard against infinite loops. However, a program that is in an infinite loop but isn't allocating memory or writing files is not particularly dangerous; it's better to leave this unlimited and just let the user kill the offending program.
In addition to the types of resources you can limit, ulimit lets you specify hard or soft limits. Hard limits can be lowered by any user but only raised by the super user (root); users can lower soft limits and raise them—but only as high as the hard limit for that resource.
If you give -H along with one (or more) of the options above, ulimit will set hard limits; -S sets soft limits. Without either of these, ulimit sets the hard and soft limit. For example, the following commands set the soft limit on file descriptors to 64 and the hard limit to unlimited:
ulimit -Sn 64
ulimit -Hn unlimited
When ulimit prints current limits, it prints soft limits unless you specify -H.