Processes and jobs
renice
Change the niceness (CPU scheduling priority) of processes that are already running, by PID, process group or user.
renice [-n] PRIORITY [-p PID... | -g PGID... | -u USER...]
nice sets the niceness when a command starts; renice changes it afterwards. The typical
moment: a backup or a runaway build is making the machine sluggish, you find its PID with
top or pgrep, and renice -n 15 -p PID pushes it down the queue without stopping it.
top can do the same interactively with the r key.
Targets are PIDs by default (-p), or every process in a process group (-g) or owned by a
user (-u). Each change is reported as PID (process ID) old priority X, new priority Y.
On Linux with util-linux 2.41, the value is absolute: renice -n 5 sets niceness 5, it does
not add 5 (--relative 5 adds). The rule for ordinary users is the same as for nice: you can
only make your own processes nicer, never less nice, even back to a value they had before.
Lowering niceness needs root (CAP_SYS_NICE). Niceness ranges from -20 (most favored) to 19.
Examples
Lower the priority of a running process
sleep 300 &
renice -n 10 -p $!
ps -o pid,ni,args -p $!
2053 (process ID) old priority 0, new priority 10
PID NI COMMAND
2053 10 sleep 300 renice reports the old and new niceness; ps confirms NI is now 10. The process keeps
running and simply gets less CPU when there is competition.
The short form without -n and -p
sleep 300 &
renice 10 $!
2056 (process ID) old priority 0, new priority 10
The priority can be given as the first argument and PIDs are the default target, so
renice 10 PID is the same as renice -n 10 -p PID.
The value is absolute, and only goes up for normal users
sleep 300 &
renice -n 5 -p $!
renice -n 3 -p $!
echo "exit status: $?"
2058 (process ID) old priority 0, new priority 5 renice: failed to set priority for 2058 (process ID): Permission denied exit status: 1
Setting 5 works. Setting 3 would make the process less nice, which an unprivileged user
may not do even for their own process: Permission denied, exit status 1. Root can move the
value either way.
Add to the current value
sleep 300 &
renice -n 5 -p $!
renice --relative 3 -p $!
2062 (process ID) old priority 0, new priority 5 2062 (process ID) old priority 5, new priority 8
--relative N adds N to the current niceness (5 + 3 = 8). Adding a negative number would
again need privileges.
Renice every process of a program
sleep 300 & sleep 301 &
renice -n 12 -p $(pgrep -x sleep)
2065 (process ID) old priority 0, new priority 12 2066 (process ID) old priority 0, new priority 12
renice has no name matching of its own; pgrep supplies the PIDs. Each process gets its
own report line.
Renice a whole pipeline (process group)
set -m
sleep 300 | sleep 301 &
renice -n 8 -g $(jobs -p %1)
ps -o pid,pgid,ni,args -C sleep
2069 (process group ID) old priority 0, new priority 8
PID PGID NI COMMAND
2069 2069 8 sleep 300
2070 2069 8 sleep 301 With job control on, a pipeline runs as one process group. -g PGID changes every member at
once; both sleep processes now show NI 8.
Renice all of a user's processes
sleep 300 &
renice -n 15 -u dev
ps -o pid,ni,args -u dev
1000 (user ID) old priority 0, new priority 15
PID NI COMMAND
2073 15 ps -o pid,ni,args -u dev
2074 15 sleep 300 -u applies to every process the user owns at that moment, including the shell running
this example, so ps, started by that shell afterwards, shows 15 as well. As root this is a quick way to calm down one
account's workload.
Processes you do not own
renice -n 10 -p 1
renice: failed to set priority for 1 (process ID): Operation not permitted
PID 1 here belongs to root; changing another user's process needs root, so renice fails
with Operation not permitted and exit status 1.
A PID that does not exist
renice -n 10 -p 99999
renice: failed to get priority for 99999 (process ID): No such process
failed to get priority ... No such process and exit status 1, worth handling in scripts
that renice a PID read from a file.
Common pitfalls
Assuming
renice -n 5adds 5. In util-linux it sets 5 (unlessPOSIXLY_CORRECTis set in the environment). Use--relativeto add, and check the reported "new priority".Trying to undo a renice as a normal user. Once you raise niceness you cannot lower it again without root, even back to 0.
Expecting threads to follow. On Linux, niceness is per thread, and
renice -p PIDchanges only the thread whose ID equals PID (the main thread); other threads keep their value. Renice each thread ID fromps -L -o lwp= -p PIDif it matters.Renicing to fix I/O-bound slowness. A process waiting on disk is not using CPU; use
ionice -c3 -p PIDinstead.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
On macOS and the BSDs,
renice PRIORITY -p PIDalso sets an absolute value, but the BSD-nmeans "increment". Writerenice 10 -p PID, without-n, if a command must work on both.
Options worth knowing
A selection, described in our own words. For the full list, see renice(1) or renice --help.
| Option | What it does |
|---|---|
-n, --priority N | Set the niceness to N (absolute in util-linux). |
--relative N | Add N to the current niceness instead of setting it. |
-p, --pid | Treat the following arguments as process IDs (the default). |
-g, --pgrp | Treat the following arguments as process group IDs. |
-u, --user | Treat the following arguments as user names or IDs. |
Related
niceRun a command with a lower (or, as root, higher) CPU scheduling priority, or print the current niceness.psPrint a snapshot of running processes, with the columns, filters and sort order you choose.topShow a live, sorted view of processes and system load; batch mode prints snapshots for logs and scripts.pgrepPrint the PIDs of processes whose name (or full command line) matches a pattern, with filters by user, parent and age.