nice and renice
Process priority for the scheduler — values from -20 to 19, start with nice -n, change with renice -p, who may raise it
Niceness is a number from -20 to 19 that tells the scheduler who should get the CPU first. The higher the number, the "nicer" the process is and the less CPU it gets when there is contention for the processor. The default is 0. nice starts a command with a changed value, renice changes it for an already running process. Useful to keep a heavy backup or log compression from getting in the way of a web server.
Syntax
nice # show the current niceness
nice [-n adjustment] command [args…]
renice [-n] value [-p] PID…
renice [-n] value -g PGID…
renice [-n] value -u user…
nice
| Form | What it does |
|---|---|
nice command | run with niceness 10 higher than the current one |
nice -n 5 command | 5 higher than the current one (the adjustment is added) |
nice -n -5 command | 5 lower — raise priority, root only |
nice -5 command | obsolete form of -n 5 (not "minus five"!) |
nice | print the niceness of the current shell |
The result is clamped to the range: nice -n 30 gives 19.
renice
| Option | What it does |
|---|---|
-n value | the new niceness (absolute, not an adjustment) |
-p PID… | for processes (arguments are treated as PIDs by default anyway) |
-g PGID… | for all processes in a group — e.g. a whole pipeline |
-u user… | for all of a user's processes |
--relative N | add N to the current value |
Prints 17 (process ID) old priority 0, new priority 5. Exit code 1 if it failed for at least one process.
Who can do what
| Action | Regular user | root |
|---|---|---|
| increase niceness of your own process (lower priority) | yes | yes |
| decrease niceness, even back to the previous value | no | yes |
| change other users' processes | no | yes |
| negative values | no | yes |
A regular user can be allowed negative values via nice in /etc/security/limits.conf. Root can do everything thanks to the CAP_SYS_NICE capability.
Where to look
| Command | What it shows |
|---|---|
ps -o pid,ni,cmd -p PID | the NI column |
ps -eo pid,ni,comm --sort=-ni | all processes, the "nicest" on top |
top | columns NI and PR (PR = 20 + NI); r in top — renice; the ni field in %Cpu(s) — CPU share of processes with positive nice |
Examples
# a heavy job with the lowest priority
nice -n 19 tar -czf /tmp/backup.tgz /etc
# the same in cron
# 0 3 * * * nice -n 19 /usr/local/bin/backup.sh
# lower the priority of an already running process
sleep 600 &
renice -n 10 -p "$!"
ps -o pid,ni,cmd -p "$!"
# all of a user's processes — to the minimum
renice -n 19 -u alice
# all processes named gzip
renice -n 15 -p $(pgrep gzip)
# child processes inherit niceness
nice -n 10 bash -c 'nice' # 10
# disk priority is a separate setting: the idle class of the I/O scheduler
ionice -c 3 nice -n 19 find / -name '*.log' > /dev/null 2>&1
Pitfalls
- Higher means lower priority.
19is the "nicest",-20the greediest. The intuition "bigger is more important" misleads you here. nice -nadds,renice -nsets.nice -n 5from a shell with niceness 10 gives 15;renice -n 5sets exactly 5 (and for a regular user that's an error — a decrease).nice -5is +5, not −5: the dash here marks an option. Write a negative value asnice -n -5ornice --5.- No permission — the command still runs.
nice -n -5 commandas a regular user prints "cannot set niceness: Permission denied", but runs the command with the old priority and returns its exit code (often 0). Don't rely onnice's exit code. - You can't go back without root. A user who raised their process's niceness to 10 can't lower it to 5.
- In a container even root usually can't set negative values: Docker drops
CAP_SYS_NICEby default. In the IzzyLab sandboxnice -n -5andrenicebelow the current value answer "Permission denied". - nice affects only the CPU, and only when there is contention for it. On an idle machine a process with 19 runs just as fast. For the disk —
ionice. - renice changes only one thread. For a multithreaded program
renice -p PIDchanges only the main thread, the others keep the old value — check withps -L -o tid,ni -p PID. It's more reliable to start the program vianiceright away: threads inherit the value when they are created.
See also
top · ps · pgrep · background · cron · capabilities