← Full reference

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

FormWhat it does
nice commandrun with niceness 10 higher than the current one
nice -n 5 command5 higher than the current one (the adjustment is added)
nice -n -5 command5 lower — raise priority, root only
nice -5 commandobsolete form of -n 5 (not "minus five"!)
niceprint the niceness of the current shell

The result is clamped to the range: nice -n 30 gives 19.

renice

OptionWhat it does
-n valuethe 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 Nadd 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

ActionRegular userroot
increase niceness of your own process (lower priority)yesyes
decrease niceness, even back to the previous valuenoyes
change other users' processesnoyes
negative valuesnoyes

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

CommandWhat it shows
ps -o pid,ni,cmd -p PIDthe NI column
ps -eo pid,ni,comm --sort=-niall processes, the "nicest" on top
topcolumns 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. 19 is the "nicest", -20 the greediest. The intuition "bigger is more important" misleads you here.
  • nice -n adds, renice -n sets. nice -n 5 from a shell with niceness 10 gives 15; renice -n 5 sets exactly 5 (and for a regular user that's an error — a decrease).
  • nice -5 is +5, not −5: the dash here marks an option. Write a negative value as nice -n -5 or nice --5.
  • No permission — the command still runs. nice -n -5 command as 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 on nice'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_NICE by default. In the IzzyLab sandbox nice -n -5 and renice below 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 PID changes only the main thread, the others keep the old value — check with ps -L -o tid,ni -p PID. It's more reliable to start the program via nice right away: threads inherit the value when they are created.

See also

top · ps · pgrep · background · cron · capabilities