EMZETT.
Login

nice, renice and ulimit: Setting limits for processes

In short: With nice you control how important a process is to the scheduler; with ulimit you limit resources such as open files or memory.

In more detail: Many processes compete for CPU time on a computer. The kernel’s scheduler distributes it by priority. With nice you can influence how friendly a process behaves, and with ulimit (resource limits) you prevent a program from consuming too many resources.

In Depth

nice and renice

The nice value ranges from −20 (highest priority) to +19 (lowest, “nice to others”). Normal is 0. Ordinary users may only raise the value, i.e. slow down their processes; only root may set lower values.

nice -n 10 ./long-job.sh           # start with low priority
renice -n 15 -p 1234               # change a running process
ionice -c3 tar czf backup.tgz /data    # low disk priority

That way a backup runs in the background without slowing the computer. The NI column in top and htop shows the value.

ulimit: resource limits

ulimit -a            # all current limits
ulimit -n            # max. open files (default often 1024)
ulimit -n 65535      # raise for this shell
ulimit -v 2000000    # max. virtual memory in KiB
ulimit -u 500        # max. processes

Soft limits apply currently and may be raised up to the hard limit. Permanently you set them in /etc/security/limits.conf or limits.d/, for services via LimitNOFILE= in the systemd unit. Servers with many connections (web servers, databases) need a high file limit, otherwise “Too many open files” appears.

cgroups: modern limiting

For finer, group-wise limits (CPU share, memory ceiling, I/O) one uses cgroups, conveniently through systemd: systemd-run --scope -p MemoryMax=500M -p CPUQuota=50% command. Container runtimes use the same technology.

Notes

The nice value only changes the weight at the scheduler; it gives no guarantee. Real-time processes (SCHED_FIFO, chrt) have their own priorities. A memory limit that is too low makes programs crash instead of getting slower; the OOM killer intervenes when the whole system runs out of memory.

See also: Processes and signals, Process scheduling, Namespaces and cgroups, Memory management and OOM killer