Linux commands
Browse topics

Adjusting process priority with nice and renice

Version
unversioned
Review
Draft

Summary#

Niceness tells the Linux scheduler how to share a busy CPU. It ranges from -20, the most favored, to 19. nice sets it for a new command and renice changes it for a running process.

Syntax#

sh
nice -n 10 ./batch-job
renice -n 15 -p "$pid"
renice -n 5 -u "$USER"
ps -o pid,ni,args -p "$pid"

nice -n adds the adjustment to the current niceness. renice -n sets the value for processes, process groups with -g or users with -u. The NI column of ps and top shows the result.

Example#

sh
(
  set -eu
  child=
  trap 'if [ -n "$child" ]; then
      kill "$child" 2>/dev/null || true
      wait "$child" 2>/dev/null || true
    fi' EXIT

  base=$(nice)
  raised=$(nice -n 5 nice)
  echo "nice added $((raised - base))"

  sleep 30 &
  child=$!
  renice -n 15 -p "$child" >/dev/null
  ps -o ni= -p "$child" | tr -d ' '
)

Expected output#

The first line reports the difference, because the starting niceness depends on the shell that runs the script.

Text
nice added 5
15

Common mistakes#

An unprivileged user can normally raise niceness but not lower it again. renice then reports that it failed to set priority for the process with Permission denied; use sudo or restart the command with the value you need. Niceness matters only when processes compete for CPU, so it does not cap CPU use and does not limit disk or memory. Use ionice for disk scheduling classes and cgroups or a service manager for hard limits.

Version applicability#

This entry describes nice from GNU Coreutils and renice from util-linux. The shell may provide its own nice builtin with a different syntax.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0