Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

top Command Guide for a Live Process Monitor

This guide helps you understand and use the top command, the classic live view of the processes consuming your system’s resources.


What is top, and Why Use It?

The top command opens a full-screen, auto-refreshing display of the processes running on your system — their CPU and memory usage, how long they have been running, and who started them. It also shows summary lines at the top: load average, task counts, and CPU states. It has been present on Unix systems for decades, so it is available on almost every machine you will ever log into.

Why Learn top?

  • Always available: Every Linux server has top, even minimal ones with no htop.
  • The system heartbeat: The load average and CPU lines tell you at a glance whether a machine is healthy or thrashing.
  • Built-in controls: Press a key to sort, kill, or renice a process without leaving the screen.
  • Scriptable: The batch mode (top -b) feeds monitoring scripts.

top is the vital-signs monitor of the Unix world: it never stops telling you how the system is doing.


1. Start the Interactive View

top
  • The screen refreshes every few seconds;
  • The header shows uptime, load average, task counts, and CPU percentages;
  • The process table below can be sorted, filtered, and acted on with single keys.

Leave top at any time with q.


2. Understand the Process Table

PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
1234 alice    20   0   928.2m   60.1m   32.5m S   7.3   1.2   0:15.42 nginx
5678 bob      20   0   1.2g  320.4m   55.3m S   2.1   6.4   3:21.07 firefox
  • PID: the process ID — the handle for kill and friends;
  • USER: who started it;
  • PR and NI: priority — see nice;
  • VIRT and RES: virtual and real memory used;
  • S: state — S sleeping, R running, D uninterruptible wait, Z zombie;
  • %CPU and %MEM: resource shares;
  • COMMAND: the program name.

3. Sort the Display by CPU or Memory

P    sort by %CPU (default)
M    sort by %MEM
T    sort by time running
<    and >   move the sort column left or right
  • Press P (shift+p) to sort by CPU and M for memory — the two most common needs;
  • After sorting, the busiest processes rise to the top of the table;
  • This is how you quickly find the runaway process.

4. Kill a Process from Inside top

k     ask for a PID and send it a signal
  • Press k, type the PID, press Enter;
  • top then asks which signal — Enter sends the default (15, polite);
  • Type 9 for SIGKILL when a process refuses to die;
  • See kill for the full picture.

5. Change Priority from Inside top

r     ask for a PID and its new nice value
  • Press r, type the PID, then the nice value from -20 to 19;
  • This is the interactive twin of renice;
  • Lowering the nice value (more negative) gives a process more CPU.

6. Filter to One User or Search by Name

u     ask for a username and show only that user's processes
o or O    add a filter field on the fly
  • Press u, type a username, and the table shows only their processes — press u again with an empty name to see everything;
  • Press o to interactively add a filter such as COMMAND=nginx;
  • Filters stack, and pressing = clears them.

7. Watch a Single Process by PID

top -p 1234
  • -p restricts the display to the given process ID;
  • Repeat the flag for several PIDs: top -p 1234 -p 5678;
  • This is how you watch one specific process without the whole table.

8. Run in Batch Mode for Scripts

top -b -n 1 | head -20
  • -b runs top in batch (non-interactive) mode;
  • -n 1 takes exactly one iteration and exits;
  • The output is plain text, so it pipes into head, grep, or a log.

Common Pitfalls

  • Confusing VIRT with real memory: VIRT includes everything the process reserved; RES is what it actually holds in RAM. Judge memory pressure by RES and the %MEM column.
  • Reading load average wrong: The three numbers are 1-, 5-, and 15-minute averages of runnable work. On a 4-core machine, a load of 4 is “fully busy,” not “broken.”
  • Forgetting the refresh: top updates its own screen; if you are reading a frozen screenshot, the numbers are old.

PageDescription
psThe snapshot view top keeps refreshing
htopThe friendlier, colorful, mouse-capable monitor
killStop processes — also possible from inside top
reniceChange priority — also possible from inside top
watchRe-run any command periodically

top Cheatsheet

TaskCommand
Start the live viewtop
Sort by CPU / memoryP / M
Kill a processk, then PID, then signal
Renice a processr, then PID, then nice value
Show one user’s processesu, then username
Watch one PIDtop -p 1234
One-shot text outputtop -b -n 1
Leaveq