Soft and hard limits
The kernel tracks resource limits with the getrlimit and setrlimit system calls. A limit has two numbers. The soft limit is the value the kernel enforces right now. The hard limit is the ceiling that the soft limit may be raised to. An unprivileged process may lower its hard limit and may raise its soft limit up to the hard limit, but it cannot raise the hard limit. Only a process with administrative rights can do that.
Limits are inherited. When a shell starts a child, the child receives the current limits of the shell. So if you lower a limit in your shell, every program you start from that shell is affected, and the change ends when the shell exits.
Reading limits with ulimit
The ulimit builtin prints the current limit for one resource. Without a flag it prints the file size limit, which is the soft value. The -S flag selects the soft value, and -H selects the hard value. The most common resource flags are -n for open files, -u for processes, -s for stack size, -v for virtual memory, and -t for CPU seconds.
ulimit -Sn
ulimit -HnThe first command prints the soft limit on open file descriptors, and the second prints the hard limit. In many containers the two numbers are different, and the soft limit is the one that starts failing first. A value of unlimited means the kernel does not enforce a limit for that resource.
To see every limit at once, use ulimit -a. The output lists each resource with its unit and its current soft value. Look for the line that says open files, and compare it with the value you read on its own.
ulimit -aSetting a soft limit in a subshell
You can lower the soft limit of the shell with ulimit -Sn 256. Doing so changes the shell for the rest of its life, which is usually not what you want. A safer habit is to run the change inside a subshell, so the parent shell keeps its own limits. The parentheses create a child shell, change its limit, and print the result, and then the child exits.
(ulimit -Sn 256; ulimit -Sn)
ulimit -SnThe first line prints 256, because the subshell lowered its soft limit. The second prints the original soft value of your shell, which is unchanged. This pattern is the usual way to test a program under a tighter limit without disturbing the rest of your session.
Lowering the hard limit cannot be undone in the same process. If you run ulimit -Hn 256 in a shell, that shell cannot raise its hard open-file limit again, so use a subshell for experiments with hard limits.
When a limit causes a failure
A process that reaches a limit receives an error from the system call, not a signal. A program that opens too many files gets EMFILE, which most programs print as a message such as too many open files. A program that creates too many processes gets EAGAIN from fork. The shell prints the message from the program and the program exits with its usual error status.
The file size limit behaves differently. If a process writes past the soft file size limit, the kernel sends SIGXFSZ, which by default ends the process. That is the one resource limit that delivers a signal, and it is useful for catching a runaway log file.
Limits also interact with each other. A process can run out of open files even when the disk has space, because the open-file limit is separate from the file system. A process can run out of processes even when memory is free, because the process limit is separate from the memory limit. When an error appears, ask which limit it came from. The error text usually names the resource, and the limits file for the process confirms the value.
Limits and services
Services are started by an init system, and their limits come from its configuration, not from your shell. A service that needs many open files sets the limit in its unit or init script. The shell ulimit is for interactive work and for scripts, where you want a clear and temporary change. When a service fails with a limit error, check the limits of the process itself, which the next lab shows you how to read.
grep "open files" /proc/$$/limitsThe file /proc/PID/limits shows the limits of any process, including the soft and hard values for each resource. This file is the source of truth, and it is the best place to look when you need the limit of a process that someone else started.
When you raise a soft limit, remember that it can never go above the hard limit. A common mistake is to write ulimit -Sn 100000 in a script and expect it to work on a machine where the hard limit is 1024. The shell reports an error, and the script continues with the old value unless it checks the exit status. A robust script reads the hard limit first, computes the largest value it may use, and then sets the soft limit to that value. Checking the status of the ulimit call is part of the same habit.
Hands-on: test a soft limit
Run the commands in the lab. The first writes a subshell limit to a file, the second writes the hard limit of your shell, and the third writes the full table so you can open it later.
(ulimit -Sn 256; ulimit -Sn) > nofile.txt
ulimit -Hn > hard.txt
ulimit -a > limits.txtThe file nofile.txt should contain 256, and the lower soft value leaves your shell unchanged. The hard value in hard.txt is whatever the lab gives your user, and it may be a number or the word unlimited. The full table lists the limit names with their values.