Menu
Modify process execution priorities
“Nice values from -20 to 19: start programs with nice, change running ones with renice or top, and read priorities in ps and top.”
A Linux machine runs many more processes than it has CPU cores. The kernel’s scheduler decides who gets the CPU next, switching between them many times per second. Most of the time that’s fair and you never notice. But sometimes a heavy job, like compressing backups or building software, slows down everything else. Then you want to tell the scheduler: “this one can wait” or “this one is important”.
That’s what the nice value is for. It’s a hint to the scheduler, and changing it is a small but very common admin task.
The nice scale#
Every process has a nice value from -20 to 19:
| Nice value | Priority | Who can set it |
|---|---|---|
-20 | Highest priority (least nice to others) | root only |
-1 to -19 | Higher than normal | root only |
0 | Normal, the default | everyone |
1 to 19 | Lower than normal (nicer to others) | everyone |
The name says it all: a process with a high nice value is nice to the others and lets them go first. A negative value means “me first”.
A new process inherits the nice value of the process that started it. Your shell normally runs at 0, so everything you start runs at 0 too. Start a shell with nice 10 and every command in it runs at 10.
The nice value isn’t a hard limit. A process at 19 still gets the whole CPU when nothing else wants it. Nice only matters when processes compete for the same CPU.Seeing priorities#
nice with no arguments prints the nice value of the current shell:
nice0To see the nice values of other processes, use ps. Pick the ni column yourself, or use the long format ps -l:
ps -eo pid,ni,user,comm | head -4
ps -l PID NI USER COMMAND
1 0 root systemd
2 0 root kthreadd
3 -20 root rcu_gp
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1000 2210 2205 0 80 0 - 2120 do_wai pts/0 00:00:00 bash
0 R 1000 2604 2210 0 80 0 - 2526 - pts/0 00:00:00 psIn top, look at the PR and NI columns:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1340 mysql 20 0 1785204 392816 23040 S 1.7 9.8 12:04.33 mariadbd
2611 amin 30 10 9620 5120 2944 R 98.0 0.1 0:12.40 xzNI is the nice value you set. PR (or PRI in ps) is the priority the kernel actually uses, calculated from it. The numbers differ per tool: top shows 20 + NI (so 20 for a normal process, 30 for nice 10), while ps -l shows 80 + NI. Lower PR means higher priority. For the exam, focus on NI: that’s the one you change.
Starting a program with a different priority: nice#
nice runs a command with a changed nice value:
nice -n 15 tar -czf /backup/home.tar.gz /home # nice 15
nice tar -czf /backup/home.tar.gz /home # no -n: adds 10, so nice 10
sudo nice -n -5 ./important-job # negative values need rootYou can see the effect by letting nice run nice, which prints its own value:
nice -n 5 nice
nice nice
nice -n 19 nice5
10
19Values outside the range are clamped: nice -n 50 gives 19. As a normal user, a negative value fails:
nice -n -5 ./important-jobnice: cannot set niceness: Permission deniedThe old syntax trap#
nice also accepts the value directly after a single dash. This is an old form, and it’s easy to misread, which makes it a favorite on the exam:
| Command | Nice value |
|---|---|
nice -n 10 cmd | 10 |
nice -10 cmd | 10 (the dash is just the option dash) |
nice -n -10 cmd | -10 |
nice --10 cmd | -10 (option dash plus a minus sign) |
Strictly speaking, nice adds the value to the current nice value. From a normal shell at 0, that’s the same as setting it.
Changing a running process: renice#
renice changes the nice value of processes that are already running. Give it the new value and the PIDs:
renice -n 5 -p 2611
renice +10 26112611 (process ID) old priority 0, new priority 5
2611 (process ID) old priority 5, new priority 10Despite the output saying “priority”, these are nice values. renice sets the value you give it (it doesn’t add to the old one), and -p (PIDs) is the default, so renice 10 2611 works too.
You can also renice everything that belongs to a user or a process group:
| Command | Changes |
|---|---|
renice -n 10 -p 2611 2612 | Those PIDs |
renice -n 10 -u amin | All processes owned by amin |
renice -n 10 -g 2600 | All processes in process group 2600 |
Who may do what#
The rules are the same as for nice, with one extra catch:
- root can set any value from -20 to 19, on any process.
- A normal user can only change their own processes, and only upwards: 0 to 5 is fine, but 5 back to 0 is refused, even if they raised it themselves a minute ago.
renice -n 3 -p 2611
renice -n 1 -p 26112611 (process ID) old priority 0, new priority 3
renice: failed to set priority for 2611 (process ID): Permission deniedRenicing from top#
In top, press r. It asks for the PID (the top process is filled in) and then for the new nice value:
PID to renice [default pid = 2611] 2611
Renice PID 2611 to value 15The NI column updates on the next refresh. The same rules apply: you need root to lower a nice value, so run sudo top if you need that.
A practical example#
A nightly backup makes the web server slow. Start it nicely from the start:
nice -n 19 tar -cJf /backup/www-$(date +%F).tar.xz /var/wwwOr, if it’s already running and hurting:
pgrep -a tar
sudo renice -n 19 -p $(pgrep -x tar)The backup takes a bit longer, but the web server stays quick, because the scheduler now lets it go first.
Tip
CPU priority doesn’t help when a job is slow because of the disk. For that there’s ionice (for example ionice -c3 cmd runs a command only when the disk is idle). It isn’t part of this objective, but it pairs well with nice in real life.
Exam checklist#
- Nice values run from -20 (highest priority) to 19 (lowest). The default is 0, and children inherit the parent’s value.
nice cmdwithout-nadds 10.nice -n 5 cmduses 5.nicealone prints the current value.- Old syntax:
nice -10 cmdmeans +10,nice --10 cmdmeans -10. - Only root can set negative values or lower a process’s nice value. Users can only raise their own.
renice -n 5 -p PID(orrenice 5 PID) changes a running process;-u userand-g groupchange many at once.- See nice values in the
NIcolumn ofps -l,ps -o niandtop.topshowsPRas 20 + NI. - In
top, pressrto renice a process.
Answer these 3 questions to commit this byte to your learning path.
Reset. This byte isn't committed any more. Answer the questions again to commit it.
$ git commit -m "learn(lpic-103): Modify process execution priorities"
[main 8294239] learn(lpic-103): Modify process execution priorities
1 byte changed, 3 insertions(+)
See it on your path Already committed . Try the questions again any time, it won't undo anything. To uncommit it and start over: