Linucate
~ Linucate_

103.6 Modify process execution priorities

All Levels

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 ps and top.
  • 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 default
  • 10 = lower priority than normal
  • 19 = 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
  • PRI is a scheduler priority field
  • NI is the niceness value you usually set directly

With top

Inside top, you can also see priority-related columns.

Useful things to notice:

  • NI = niceness
  • PR = 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
  1. Run: nice -n 10 sleep 120
  2. In another shell, inspect: ps -eo pid,comm,ni,pri | grep sleep
  3. Notice the NI value.
  4. Remember: this safe demo shows the field clearly, even though sleep is not CPU-heavy.
2) Change niceness of a running process
  1. Find the PID: pgrep -a sleep
  2. Change it: sudo renice 5 -p <pid>
  3. Verify with: ps -eo pid,comm,ni,pri | grep <pid>
3) Read priority fields in `top`
  1. Start: top
  2. Look for the PR and NI columns.
  3. Compare them with what ps showed.

Cheat Sheet

  • default niceness is usually 0
  • niceness range is usually -20 to 19
  • higher niceness = lower CPU preference
  • lower niceness = higher CPU preference
  • nice -n <value> command = start with custom niceness
  • renice <value> -p <pid> = change niceness of running process
  • ps -eo pid,comm,ni,pri = show niceness and priority fields
  • top = live view of priority-related fields
🎯

Test Your Knowledge

Complete the quiz to assess your understanding of this course's concepts.