Introduction
This lesson is about process priority.
The main question is:
How do you let one process run with more or less CPU preference than another?
What you should be able to do after this lesson:
- Understand niceness values and their range.
- Start a process with a custom niceness.
- Change niceness of a running process.
- See priority information in
psandtop. - Avoid common beginner mistakes about process priority.
Big Idea: Niceness Is a Hint About CPU Preference
When several runnable processes want CPU time, Linux must choose who gets more attention.
At beginner level, the picture is:
- lower niceness = higher CPU preference
- higher niceness = lower CPU preference
That is why the name is slightly funny:
- a "nice" process is being polite
- it gives other processes more chance to run first
If only one process wants CPU time, niceness may not feel dramatic. Its effect matters more when several busy processes compete.
Normal Range and Default
The usual niceness range is:
-20 ... 19
Common meaning:
0= normal default10= lower priority than normal19= very low priority- negative values = more favored by the scheduler
The usual default niceness for a new process is:
0
LPIC-1 likes this detail because nice and renice are always understood relative to that normal starting point.
Start a Process with nice
Lower priority than normal
nice -n 10 long-command
Very low priority
nice -n 19 backup-command
Higher priority than normal
nice -n -5 long-command
This usually needs more privilege.
Simple rule:
- positive number = be more polite
- negative number = ask for more CPU preference
Change a Running Process with renice
sudo renice 5 -p 1234
sudo renice -5 -p 1234
Read it as:
- change the niceness of PID
1234
This is useful when the process is already running and you do not want to restart it.
See Priority Information
With ps
ps -eo pid,comm,ni,pri --sort=ni | head
Example output from a low-priority test process:
PID COMMAND NI PRI
13 sleep 10 9
What this shows:
- the process niceness is
10 - that is lower priority than the default niceness
0 PRIis a scheduler priority fieldNIis the niceness value you usually set directly
With top
Inside top, you can also see priority-related columns.
Useful things to notice:
NI= nicenessPR= priority field
You do not need every scheduler detail here. The main skill is reading these fields without confusion.
Real-Life Use Cases
Niceness is useful when you want background work to stay out of the way.
Common examples:
- backups
- compression jobs
- large indexing tasks
- batch processing
Simple everyday idea:
- interactive work should stay responsive
- background work can often be more polite
A Simple Way to Think About It
- a higher niceness value is good for "do this eventually" work
- a lower niceness value may help important CPU-heavy work compete better
- niceness matters most when several CPU-hungry jobs run at the same time
But for beginners:
do not change priorities without a reason
Practice Step by Step
1) Start a safe low-priority test process
- Run:
nice -n 10 sleep 120 - In another shell, inspect:
ps -eo pid,comm,ni,pri | grep sleep - Notice the
NIvalue. - Remember:
this safe demo shows the field clearly, even though
sleepis not CPU-heavy.
2) Change niceness of a running process
- Find the PID:
pgrep -a sleep - Change it:
sudo renice 5 -p <pid> - Verify with:
ps -eo pid,comm,ni,pri | grep <pid>
3) Read priority fields in `top`
- Start:
top - Look for the
PRandNIcolumns. - Compare them with what
psshowed.
Cheat Sheet
- default niceness is usually
0 - niceness range is usually
-20to19 - higher niceness = lower CPU preference
- lower niceness = higher CPU preference
nice -n <value> command= start with custom nicenessrenice <value> -p <pid>= change niceness of running processps -eo pid,comm,ni,pri= show niceness and priority fieldstop= live view of priority-related fields
Test Your Knowledge
Complete the quiz to assess your understanding of this course's concepts.
