Bash ulimit builtin command

Updated: 05/04/2019 by Computer Hope
ulimit command

On Unix-like operating systems, ulimit is a builtin command of the Bash shell. It displays or sets resource limits of the shell.

Description

Resources that can be limited include the size of files written, how much memory may be allocated, and how many files may be open at once.

The average user on a laptop or desktop Linux computer may never need to alter their default resource limits. However, if you are operating a server with many high-performance processes, or a high number of simultaneous users, limiting resources may be necessary. By limiting resources, you can prevent any single process or user session from slowing down or interfering with the rest of the system.

Note

On Linux systems that use the systemd or upstart initialization systems, ulimit settings might be ignored. On these systems, use prlimit instead.

Syntax

ulimit [-S] [-H] [-a] [-b] [-c] [-d] [-e] [-f] [-i] [-k] [-l]
       [-m] [-n] [-p] [-q] [-r] [-s] [-t] [-u] [-v] [-x] [-P] [-T] limit

Some options allow you to specify a new limit. This can be an appropriate numeric value, or the special values soft, hard, or unlimited.

  • The value soft refers to the "soft" resource limit. This is a limit which may be exceeded if necessary for short periods of time, but never exceed the hard limit.
  • The value hard refers to the "hard" resource limit. The hard resource limit may never be exceeded under any circumstances.
  • The value unlimited means that there are no limitations on that resource.

If you include the -S or -H option, all other options will refer to the soft or hard limit, respectively. If you specify neither or both of these options, -S is assumed.

Some options, but not all, will report the current limit if you omit the limit option.

Options

The ulimit builtin command takes the following options.

Note

The options available to ulimit are system dependent. Run "help ulimit" to check which options are available on your system.

-S Use the "soft" resource limit for all operations. Soft limits can be exceeded for a short period of time, if necessary, but may never exceed a hard limit.
-H Use the "hard" resource limit for all operations. Hard limits can never be exceeded.
-a Report all current resource limits.
-b [limit] Specify the size of the socket buffer.
-c [limit] Specify the maximum size, in blocks, of core files. If limit is omitted, the current limit is reported.
-d [limit] Specify the maximum size of a process's data segment, in kilobytes. The data segment is the read-write portion of a process's address space which contains initialized global and static local variables. If limit is omitted, the current limit is reported.
-e [limit] Specify the maximum scheduling priority, aka "nice".
-f [limit] Report the maximum size in blocks of files written by the bash shell, and any of its children. This is the default option if no others are specified.
-i [limit] Set the maximum number of pending signals. If limit is omitted, the current limit is reported.
-k limit Set the maximum number of kqueues allocated for the current process. In Linux, kqueues are usually handled by the inotify API. See man inotify for more information.
-l [limit] Set the maximum size, in kilobytes, that a process may occupy in memory (including RAM, swap, and other file system memory allocation). If limit is omitted, the current limit is reported.
-m [limit] Set the maximum resident set size, in kilobytes. This is similar to -l, but applies only to the portion of process memory resident in RAM. If limit is omitted, the current limit is reported.
-n [limit] Set the maximum number of open file descriptors. If limit is omitted, the current limit is reported.
-p [limit] Set the pipe size, in pages. (Memory page size is kernel dependent and defined in the source header file pipe_fs_i.h.) If limit is omitted, the current limit is reported.
-q [limit] Set the maximum number of bytes in POSIX message queues. If limit is omitted, the current limit is reported.
-r [limit] Set the maximum real-time scheduling priority (-20 to 20). See also -e, above. If limit is omitted, the current limit is reported.
-s [limit] The maximum size, in kilobytes, of the shell's stack. If limit is omitted, the current limit is reported.
-t [limit] The maximum amount of CPU time, in seconds, which may be used by a process. If limit is omitted, the current limit is reported.
-u [limit] The maximum number of processes that may be owned by a user. If limit is omitted, the current limit is reported.
-v [limit] The size, in kilobytes, to be used for virtual memory. If limit is omitted, the current limit is reported.
-x [limit] The maximum number of file locks which may be issued. If limit is omitted, the current limit is reported.
-T limit Set the maximum number of threads.

Exit status

The ulimit command returns 0 for success, and 1 if anything goes wrong, such as invalid options or a permissions error.

Examples

ulimit

Report the soft limit on file size.

ulimit -f

Same as the above command.

ulimit -Hf

Report the hard limit on file size.

ulimit -H

Same as the above command.

ulimit -a

Report all soft resource limits.

ulimit -Sa

Same as the above command.

ulimit -Ha

Report all hard resource limits.

ulimit -u

Report the soft limit on number of user processes.

ulimit -u 8000

Set the soft limit on number of user processes to 8000.

vimdiff <(ulimit -Sa) <(ulimit -Ha)

Use vimdiff to compare soft limits and hard limits. The less-than symbol with parentheses <( ... ) is an example of bash process substitution. The ulimit commands are run in subshells, and the output of each command is read by vimdiff as if from files. The soft limits are shown on the left and hard limits on the right, as in the screenshot below.

Screenshot: Using vimdiff to compare hard and soft ulimits.

df — View used and available disk space.
free — View used and available memory.