Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

nice Command Guide for Starting Programs at Lower Priority

nice runs a command with a chosen CPU priority — a higher “nice” value means the program yields more to everyone else.


What is nice, and Why Use It?

The nice command starts a program with a niceness value set: a number from −20 (greedy, takes priority) to 19 (polite, yields to others). The scheduler uses niceness to decide how CPU time is shared — a niced-down backup job will not starve the editor you are typing in. nice is how you set that priority before the program starts; renice changes it afterward.

Why Learn nice?

  • Be a good citizen: long batch jobs (compiles, backups, transcodes) should not hog CPU from interactive work.
  • Protect interactive work: running something important? nice -n -10 (needs privileges) or lower others with renice.
  • No config needed: priority is set per-invocation, on the command line.
  • Pairs with renice: nice for launch-time, renice for mid-flight — one concept, two commands.

nice is the politeness knob of Linux: the higher the number, the more the program says “excuse me” to the scheduler.


1. Basic Usage: Run Niced-Down

# Start a backup at niceness 10 (very polite)
nice -n 10 ./backup.sh

# Without -n, nice uses its default increment of 10:
nice ./job.sh        # same as nice -n 10 ./job.sh
  • nice cmd without -n starts the command at niceness 10 (the default increment).
  • nice -n 19 cmd is the most polite setting — the program yields to nearly everything.

See the niceness of a running process with ps:

ps -o pid,ni,cmd -p $(pgrep -f job.sh)
  • The NI column shows the niceness value; 0 is the neutral default.

2. The Niceness Scale

nice -n -5 ./important.sh   # greedy — needs privileges (see below)
nice -n  0 ./normal.sh      # neutral
nice -n 19 ./polite.sh      # yields to everyone
NicenessMeaning
−20Highest priority (only root may set)
0Default
19Lowest priority
  • Regular users may only increase niceness (0 → 19); lowering it (negative) requires root.
  • Niceness affects CPU scheduling only — it does not throttle disk or network use.

3. Running with sudo for Higher Priority

# Run at niceness -5 (needs root)
sudo nice -n -5 ./critical-job.sh

# Or use renice on an already-running process instead
  • The rule: raising priority (negative niceness) requires root; lowering it (positive) does not.
  • Prefer renice for adjusting processes that are already running.

4. Combining with Pipes and Scripts

# Niced-down pipeline
nice -n 15 tar -czf backup.tar.gz /home | \
  nice -n 15 gzip > /mnt/backup/backup.tar.gz

# In a script: run everything politely
for f in *.raw; do
  nice -n 10 convert "$f" "${f%.raw}.png"
done
  • Each nice applies to the command it starts; pipelines need it per stage or wrapped in a subshell.

5. nice vs. renice

SituationCommand
Starting a new program with priority setnice
Changing priority of a running programrenice
nice -n 10 ./long-job.sh &      # start polite
renice -n 5 -p $(pgrep long-job)  # later, make it a bit greedier
  • nice is launch-time; renice is mid-flight. Both move the same NI value.

Common Pitfalls

  • Negative niceness silently fails without root: nice -n -5 cmd as a normal user runs at niceness 10 anyway (or errors) — check with ps.
  • Niceness is not a hard cap: a niced program still gets CPU when nothing else wants it; it only loses contests.
  • It does not affect I/O or network: for disk-throttled copies use ionice; niceness is CPU-only.
  • Children inherit niceness: a niced shell passes its politeness to everything it spawns.
  • nice the command vs. “nice” the value: the command sets the value at launch; the value is what renice edits later.

PageRelationship
reniceChanges priority of running processes — the mid-flight twin.
psps -o ni shows the niceness of any process.
topLive view includes the ni column.
nohupCombine with nice for polite, logout-proof jobs.
jobsTrack the niced job you backgrounded.

nice Cheatsheet

CommandWhat It Does
nice cmdStart cmd at niceness 10.
nice -n 19 cmdStart at the lowest priority.
nice -n 5 cmdStart slightly more polite than default.
sudo nice -n -5 cmdStart at high priority (root only).
ps -o ni,cmdSee niceness values.
renice -n 5 -p pidChange priority of a running process.