Skip to content

Linux Foundations / Manage a Linux host ยท Step 33 of 50

Lesson: Watching processes live with topSwitch
โ† Linux Foundations

System administration

Watching processes live with top

Use top to see which processes are busy and to read the system summary.

ps gives you one snapshot. top shows a live view that refreshes every few seconds, which makes it a good first tool when a machine feels slow.

Reading the screen

bash
$ top
top - 10:15:02 up 3 days,  2:11,  2 users,  load average: 0.42, 0.31, 0.25
Tasks: 213 total,   1 running, 212 sleeping,   0 stopped,   0 zombie
%Cpu(s):  3.1 us,  1.0 sy,  0.0 ni, 95.6 id,  0.3 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7850.0 total,   2150.0 free,   2900.0 used,   2800.0 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   4950.0 avail Mem

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
   1893 maria     20   0 3401220 512300 120400 S   6.0   6.4   4:12.55 firefox

The top lines summarise the machine: uptime, load averages, task counts, CPU time and memory. Below is one row per process. RES is physical memory in use, S is the state, and TIME+ is the total CPU time used. Layout and units vary a little between versions.

Keys to know

While top is running:

  • P sorts by CPU use, M by memory (both are capital letters)
  • 1 shows each CPU separately
  • k sends a signal to a PID you type
  • r changes a process's niceness
  • h shows help
  • q quits

CPU above 100%

In what top calls Irix mode, %CPU for a process is measured against one CPU. A multi-threaded program using two full cores can show 200%. Pressing I (capital i) toggles between this and Solaris mode, which divides by the number of CPUs.

Useful options

bash
top -u maria          # one user's processes
top -p 1893           # one PID only
top -b -n 1 > top.txt # batch mode, one refresh

Batch mode (-b) prints plain text, so you can save or pipe it. -n limits the number of refreshes and -d sets the delay in seconds.

bash
$ top -b -n 1 | head -3
top - 10:16:40 up 3 days,  2:13,  2 users,  load average: 0.30, 0.30, 0.25
Tasks: 212 total,   1 running, 211 sleeping,   0 stopped,   0 zombie
%Cpu(s):  2.0 us,  0.7 sy,  0.0 ni, 97.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

htop is a friendlier alternative with colours and scrolling, but you usually have to install it.

Try it

Open top, press M and then P, and watch the order change. Press 1, then q. In another terminal, run yes > /dev/null and find it in top; stop it with Ctrl+C when done. The processes and cron lab is a safe place for this.

Check your understanding

Watching processes live with top

All 4 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.