top
Interactive live view of top resource-consuming processes
By CMD Script Team · 4 min read · Last updated
top [OPTIONS]Options
| Flag | Description |
|---|---|
-d | Set the refresh delay in seconds between updates, e.g. -d 2 |
-n | Exit after a fixed number of iterations, e.g. -n 1 for a single snapshot |
-b | Batch mode: non-interactive output suitable for logging or piping to a file |
-u | Show only processes owned by a given user, e.g. -u www-data |
-p | Monitor only specific PIDs, e.g. -p 1234,5678 |
-o | Sort by a given field on startup, e.g. -o %CPU or -o %MEM |
-H | Show individual threads instead of summarizing per process |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS (BSD top, different key bindings/columns)
What it does
top displays a continuously updating, full-screen view of running processes ranked by
resource usage, along with a summary of overall CPU, memory, swap, and load-average
stats at the top of the screen. It's the classic first tool for answering "what's eating
my CPU/RAM right now" on a Linux system, and it lets you interact with the process list —
sorting, filtering, and even killing processes — without leaving the terminal.
Beginner examples
top— launch the interactive view, refreshing every 3 seconds by default- Press
qto quit - Press
Pto sort by CPU usage,Mto sort by memory usage - Press
uthen type a username to filter to that user's processes
top
Advanced examples
- Start already sorted by memory usage:
top -o %MEM - Take one snapshot and pipe it through other tools:
top -bn1 | head -15 - Watch only specific PIDs (e.g. your app's worker processes):
top -p 1234,1235,1236 - Show individual threads of multi-threaded processes:
top -H -p 1234 - Log resource usage over time to a file for later analysis:
top -b -d 5 -n 12 > usage.log
top -bn1 -o %CPU | head -15
Common mistakes
- Reading
%CPUas a percentage of total system capacity rather than per-core; on an 8-core box, 800% means the box is fully saturated, not "way over 100%." - Confusing
RES(resident memory actually in RAM) withVIRT(total virtual memory address space, which can be huge and mostly unused) when judging real memory pressure. - Forgetting that
top's default view only shows a screenful of processes — leaving out quieter processes that may still matter for a specific investigation; use-pto pin ones you care about. - Trying to script around interactive
topoutput directly instead of using-b(batch mode), which produces unstable, screen-redraw-laden text that's hard to parse.
Tips
- Press
1to toggle between an aggregate CPU summary line and a per-core breakdown — essential for spotting a single-threaded process pegging one core on a multi-core box. - Press
kto kill a process by PID directly from the UI, andrto renice (change priority) without switching to another terminal. - Press
cto toggle showing the full command line (with arguments) instead of just the process name — useful for distinguishing multiple instances of the same binary. - Press
Wto save your current display settings (sort order, columns, delay) to~/.toprcso they persist next time you launchtop.
Best practices
- Use
top -bn1(batch, one iteration) in scripts and cron jobs rather than the interactive mode, which assumes a terminal and will hang or misbehave otherwise. - When comparing CPU usage across processes, sort with
P(or-o %CPU) instead of eyeballing an unsorted list. - For long-running monitoring or historical graphs, prefer a proper time-series tool
(
sar,vmstat, or a metrics agent) —topis best for real-time, ad hoc triage. - Check load average alongside
%CPU; a high load average with low per-process CPU% often points to processes blocked on I/O rather than compute-bound work.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Quickly diagnosing a server that feels slow by checking which process is consuming CPU or memory right now, during an incident.
- Killing a runaway process directly from
top(kthen PID) without needing a second terminal to runpsandkill. - Watching memory usage climb during a suspected memory leak by sorting with
Mand observingRESgrow for a specific process over several refreshes.
Common interview questions
- What's the difference between RES and VIRT in top's output?
VIRTis the total virtual address space a process has mapped (including shared libraries, memory-mapped files, and unused reserved space);RESis the actual physical RAM currently in use — RES is the more meaningful number for real memory pressure. - How would you find and kill the process using the most memory, without leaving top?
Sort with
M(or start withtop -o %MEM), note the PID at the top, pressk, enter the PID, and confirm the signal. - How do you use top in a non-interactive script?
top -bn1runs top in batch mode for a single iteration and prints plain text output that can be piped, grepped, or logged.
Frequently Asked Questions
How do I kill a process directly from top?
Press k, type the PID when prompted, then press Enter and confirm the signal (default 15/SIGTERM, or type 9 for SIGKILL).
How do I sort by CPU or memory usage?
Press P to sort by %CPU or M to sort by %MEM interactively, or start top with -o %CPU / -o %MEM to sort from launch.
How do I make top run once and exit, for use in scripts?
Use top -bn1, which runs in batch mode for a single iteration and prints plain text instead of the interactive UI.
Why does top's CPU% sometimes add up to more than 100%?
On multi-core systems top can report per-process CPU usage relative to a single core, so a process using two full cores shows 200%. Press 1 in top's summary area to toggle showing per-core breakdown instead of the aggregate.
Cheat sheet
Download a quick-reference cheat sheet for top.