top shows a system summary (uptime, load average, CPU, memory) and a process list that refreshes every 3 seconds. By default processes are sorted by CPU usage — the first thing to look at when "the server is slow". In batch mode (-b) top prints snapshots to stdout, so you can save its output or parse it in a script.
Syntax
top [options]
top -b -n 1 # one snapshot, non-interactive
Command-line options
| Option | What it does |
|---|
-b | batch mode: no keyboard control, output to stdout |
-n number | how many refreshes to do before exiting |
-d seconds | refresh interval, may be fractional: -d 0.5 |
-p PID[,PID…] | only these processes (up to 20) |
-u user | only the user's processes (by effective UID); -U — by any of the UIDs |
-o field | sort by a field: %CPU, %MEM, RES, TIME+, PID; -o -PID / +PID — ascending / descending |
-c | show the full command line instead of the name |
-H | show threads rather than processes |
-1 | a separate line for each CPU core |
-E k|m|g | memory units in the summary; -e k|m|g — in the process list |
-w number | output width in batch mode (otherwise lines are truncated) |
-i | hide processes that are doing nothing |
-s | secure mode: no k and r |
-O | list all field names for -o |
Keys in interactive mode
| Key | What it does |
|---|
q | quit |
h or ? | help |
P / M / T / N | sort by CPU / memory / CPU time / PID |
< / > | move the sort column left / right |
R | reverse the sort order |
k | send a signal to a process: asks for the PID and the signal (default 15 SIGTERM) |
r | change a process's nice value (renice) |
u | show only a user's processes (empty — everyone's) |
o / O | filter: COMMAND=nginx, %CPU>5.0; = — reset filters |
L / & | find a string / next match |
c | full command line on/off |
1 | per-core usage on/off |
H | threads on/off |
V | process tree (parent → children) |
i | hide idle processes |
d or s | change the refresh interval |
E / e | change memory units in the summary / in the list |
f | choose and order columns |
x / y | highlight the sort column / running processes |
l / t / m | hide/show the load average / CPU / memory line |
W | save the current settings to ~/.config/procps/toprc |
| space | refresh now |
Reading the summary
top - 15:29:20 up 19:38, 1 user, load average: 5.28, 6.35, 5.84
Tasks: 104 total, 1 running, 103 sleeping, 0 stopped, 0 zombie
%Cpu(s): 12.0 us, 9.6 sy, 0.0 ni, 73.5 id, 1.2 wa, 0.0 hi, 3.6 si, 0.0 st
MiB Mem : 3918.8 total, 640.2 free, 2259.3 used, 1237.6 buff/cache
MiB Swap: 4096.0 total, 2968.7 free, 1127.3 used. 1659.5 avail Mem
| Field | Meaning |
|---|
up 19:38 | how long the system has been running |
load average | average CPU queue length over 1, 5 and 15 minutes: processes running or waiting for the CPU, plus those waiting for disk (state D) |
us / sy | CPU time spent on user code / on the kernel |
ni | on processes with raised nice (low priority) |
id | idle |
wa | waiting for I/O — a high value = the disk is the bottleneck |
hi / si | handling hardware / software interrupts |
st | time "stolen" from the virtual machine by the hypervisor |
free / used / buff/cache | free / used / file cache (freed on demand) |
avail Mem | how much memory is really available to new programs — look at this, not at free |
Load average is compared with the number of cores (nproc): on 4 cores 4.00 is full utilization, 8.00 — a queue twice as long as the number of cores. A rising first number — load is growing, a falling one — load is easing.
Process columns
| Column | Meaning |
|---|
PID / USER | process number / effective user |
PR | scheduler priority (20 + NI for normal processes; rt — real-time) |
NI | nice: from -20 (high priority) to 19 (low) |
VIRT | all virtual memory, including what isn't loaded (KiB) |
RES | physical memory actually used (KiB) — the key number |
SHR | the part of RES shared with other processes (libraries) |
S | state: R running, S sleeping, D waiting for disk (uninterruptible), T stopped, t under a debugger, Z zombie, I idle kernel thread |
%CPU | CPU usage over the last interval; on several cores it can exceed 100 |
%MEM | RES as a percentage of total memory |
TIME+ | total CPU time, with hundredths |
COMMAND | program name (c — full command) |
Examples
# a snapshot to a file: the first 20 lines
top -b -n 1 | head -20 > snapshot.txt
# top 5 by memory
top -b -n 1 -o %MEM | sed -n '8,12p'
# watch specific processes
sleep 300 &
top -p "$!"
top -b -n 3 -d 1 -p "$(pgrep -d, sleep)"
# one user's processes with full commands, wider than the screen
top -b -n 1 -u www-data -c -w 200
# record the load every 5 seconds for a minute
top -b -d 5 -n 12 | grep '^top -' >> load.log
# only the load average — without top
cat /proc/loadavg
uptime
Pitfalls
- The first
top -b -n 1 snapshot is inaccurate for %CPU: the percentages have nothing to compare against. For honest numbers, take two refreshes and use the last one: top -b -n 2 -d 1.
- A small
free is normal. Linux uses free memory for the file cache. A memory shortage shows up as a small avail Mem and a growing used in swap.
VIRT isn't memory consumption. It's the whole address space, including reserved and untouched parts. Look at RES.
- Load average isn't CPU percent. Processes in state
D (waiting for disk or NFS) raise the load even if the CPU is idle. High load with a large id and a large wa — a disk problem.
- In a container
top shows the load average, CPU and memory of the whole host, but only its own processes. The container's limits aren't visible there.
- Without
-w, lines in batch mode are truncated to the terminal width (or 80 characters), and long commands are lost.
-p takes a comma-separated list, not space-separated: pgrep -d, name gives the right format.
See also
ps · kill · nice · pgrep · proc · system-info