Intermediate · 10 min · practise every command in the built-in terminal

Priorities — nice and renice

Two programs want the CPU: who gets it? The kernel shares it out by niceness, from -20 (first in line) to 19 (gives way to everyone). Almost everything runs at 0.

Something is eating a whole CPU right now:

top
    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
   3412 student   20   0    7880   2140   1284 R  98.7   0.1   6:41.00 gzip

NI is its niceness; PR the priority the kernel works with (20 + NI).

ps -o: the columns you want

ps -o pid,ni,%cpu,comm -p 3412
    PID  NI %CPU COMMAND
   3412   0 98.7 gzip

-o picks the columns: pid, user, ni, %cpu, %mem, rss, etime, cmd… -p the process.

renice: a process that runs

That gzip packs old photos — it can wait. Make it nicer:

renice -n 10 -p 3412
3412 (process ID) old priority 0, new priority 10

When nothing else wants the CPU it runs as fast as before; it only lets the web server go first.

WhoMay
any usermake their own processes nicer (a higher number)
rootanything — also lower the number, below 0

Going back from 10 to 0 is "less nice" — only root may: renice -n 0 -p 3412 → Permission denied.

nice: start it nice

nice -n 19 tar -czf ~/docs.tar.gz ~/documents

Runs the command at niceness 19. nice alone prints the niceness you run at: 0.

💡 For a job that runs every night (backups, reports): nice -n 10 in front of it in the crontab, or Nice=10 in its systemd unit.

Practice tasks

  1. Find the process that takes a whole CPU
  2. See its niceness and CPU use
  3. Make it give way: niceness 10
  4. Try to take that back as a normal user
  5. See the niceness your shell runs at
  6. Pack ~/documents as nicely as possible

Commands in this lesson

Processes