16.6 nice and renice: Adjusting Process Priority
Right, so you’ve got a process hogging your CPU. Maybe it’s a runaway script, maybe it’s ffmpeg trying to turn the entire Lord of the Rings trilogy into a single GIF. You don’t necessarily want to kill it; you just want to politely ask it to take its foot off the gas pedal so the rest of your system can breathe. This is where nice and renice come in. They’re your tools for adjusting a process’s priority in the CPU’s scheduling queue.
Think of the CPU scheduler as a harried bartender at a ridiculously busy pub. Processes are the patrons shouting for attention. The scheduler has to decide who gets served next. A process’s “nice-ness” is basically how politely it’s willing to wait its turn. A lower nice value means the process is less nice—it’s a demanding, impatient patron who gets served first. A higher nice value means the process is more nice and is content to wait, letting others go ahead.
The nice value ranges from -20 (the absolute least nice, most urgent priority) to 19 (the nicest, most “after you” priority). It’s counterintuitive until you think of it as “how nice is this process being to the others?” and then it makes perfect, sadistic sense.
Starting a Process with a Custom Niceness
You use the nice command to launch a process with a specific priority from the get-go. By default, processes start with a nice value of 0. The syntax is:
nice -n [NICENESS] [COMMAND]
Let’s say you’re about to run a heavy data compression job with zip. You know it’s going to be a resource hog, so you decide to be a good citizen and start it with a high nice value of 15.
nice -n 15 zip -r massive_archive.zip /a/large/directory/
Now, that zip process will happily chug along in the background, but it will immediately yield the CPU to any process with a nicer (lower) value, like your shell (which is probably at 0) or your GUI. The system feels responsive because your interactive tasks are inherently more urgent than a background batch job.
Crucial Pitfall: Regular users are restricted. You can only increase the nice value (make a process more nice). You can’t make a process less nice than 0. If you try to set a negative value, it will simply fail unless you’re root. This is the system’s way of stopping you from being a jerk and hogging all the resources.
$ nice -n -5 some_command
nice: cannot set niceness: Permission denied
# Time to sudo!
$ sudo nice -n -15 some_critical_daemon
Changing Priority on the Fly with renice
But what if you already started that massive compilation job (make -j12) and your system is now crawling? You don’t need to stop it and restart it; you can adjust its priority live with renice.
The syntax targets a running process by its PID (Process ID). First, find the PID of the offending process. pgrep is your friend here.
pgrep -l make
# Output: 4491 make
Now, let’s make that make process much nicer to everyone else, say to a value of 19.
renice -n 19 -p 4491
Or, if you’re feeling particularly ruthless and want to demote every process owned by a user named data_scrubber, you can do that too.
sudo renice -n 15 -u data_scrubber
The beauty of renice is that it’s immediate. You’ll feel the system become responsive again as the CPU-intensive process is effectively throttled back.
The Cold, Hard Truth About Niceness
Here’s the part most manuals gloss over: nice and renice are only suggestions to the scheduler. They are not hard limits. If the system is completely idle, your nice-19 process will still use 100% of the CPU. These commands only matter when there is contention for CPU time. If no one else is asking for the CPU, the nice process might as well take it.
Furthermore, they only adjust CPU priority. They have precisely zero effect on I/O priority (disk or network access). A nice process can still bring your disk I/O to a grinding halt. This is a massive shortcoming and why, on modern systems, the ionice command often needs to be used in tandem with nice for true resource control. But that’s a story for another section.
The bottom line? Use nice for any long-running, non-interactive job you start. Use renice as your emergency brake when something’s already gotten out of hand. And remember, with great power (and sudo access) comes the responsibility to not set everything to -20.