# 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.

Source: https://aminbytes.dev/bytes/gnu-and-unix-commands/process-priorities/


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.

 (Side note: 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:

```bash
nice
```

```text
0
```

To see the nice values of other processes, use `ps`. Pick the `ni` column yourself, or use the long format `ps -l`:

```bash
ps -eo pid,ni,user,comm | head -4
ps -l
```

```text
    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 ps
```

In `top`, look at the `PR` and `NI` columns:

```text
    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 xz
```

`NI` 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:

```bash
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 root
```

You can see the effect by letting `nice` run `nice`, which prints its own value:

```bash
nice -n 5 nice
nice nice
nice -n 19 nice
```

```text
5
10
19
```

Values outside the range are clamped: `nice -n 50` gives 19. As a normal user, a negative value fails:

```bash
nice -n -5 ./important-job
```

```text
nice: cannot set niceness: Permission denied
```

### The 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:

```bash
renice -n 5 -p 2611
renice +10 2611
```

```text
2611 (process ID) old priority 0, new priority 5
2611 (process ID) old priority 5, new priority 10
```

Despite 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.

```bash
renice -n 3 -p 2611
renice -n 1 -p 2611
```

```text
2611 (process ID) old priority 0, new priority 3
renice: failed to set priority for 2611 (process ID): Permission denied
```

## Renicing from top

In `top`, press `r`. It asks for the PID (the top process is filled in) and then for the new nice value:

```text
PID to renice [default pid = 2611] 2611
Renice PID 2611 to value 15
```

The `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:

```bash
nice -n 19 tar -cJf /backup/www-$(date +%F).tar.xz /var/www
```

Or, if it's already running and hurting:

```bash
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 cmd` without `-n` adds **10**. `nice -n 5 cmd` uses 5. `nice` alone prints the current value.
- Old syntax: `nice -10 cmd` means +10, `nice --10 cmd` means -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` (or `renice 5 PID`) changes a running process; `-u user` and `-g group` change many at once.
- See nice values in the `NI` column of `ps -l`, `ps -o ni` and `top`. `top` shows `PR` as 20 + NI.
- In `top`, press `r` to renice a process.


## Byte check

1. What nice value does `nice backup.sh` (no other options) give the script?
   - `-10`
   - **`10`** (correct)
   - `19`
   Without `-n`, `nice` adds 10 to the current nice value. Started from a normal shell (nice 0), that's 10.

2. As a normal user, you ran a job with nice 10. What happens with `renice -n 5 -p <PID>`?
   - It works: you own the process
   - **Permission denied: only root can lower a nice value** (correct)
   - It sets the nice value to 15
   Lowering the nice value raises the priority. Normal users may only make their processes nicer (higher numbers), never back down.

3. Which nice value gives a process the **highest** CPU priority?
   - `19`
   - `0`
   - **`-20`** (correct)
   The scale runs from -20 (least nice, highest priority) to 19 (nicest, lowest priority). 0 is the default.


