Linux Command Reference ยท Processes and Signals
Linux top command
Reference for the Linux top command: read the summary lines, sort by CPU or memory, use batch mode for scripts, and learn the interactive keys.
top is a live, refreshing view of the machine: a few summary lines (uptime, load, tasks, CPU and memory) above a table of processes sorted by CPU use. Unlike ps it keeps updating, usually every three seconds, so you see what is busy now.
It is interactive by default: you press keys to sort, kill processes or change what is displayed. For scripts and logs there is batch mode (-b), which prints plain text instead of drawing on the screen. Many people also use htop, a friendlier alternative that is not installed everywhere; top is present on practically every Linux system.
top top -b -n 1 | head -15
Try it
In the sandbox there is no terminal to draw on, so use batch mode with a fixed number of iterations.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
top [-b] [-n COUNT] [-d SECONDS] [-p PID,...] [-u USER] [-o FIELD] [-H]
| Option | Description |
|---|---|
-b |
Batch mode: plain text, no keyboard input, suitable for pipes and files. |
-n COUNT |
Quit after COUNT updates. |
-d SECONDS |
Delay between updates (fractions allowed, for example 0.5). |
-p PID,... |
Show only these process IDs. |
-u USER |
Show only processes of this user. |
-o FIELD |
Sort by FIELD (for example %MEM, TIME+); -FIELD for ascending. |
-H |
Show individual threads instead of processes. |
-c |
Show full command lines. |
-i |
Hide idle processes. |
Reading the display:
load average: 0.50, 0.40, 0.30is the average number of runnable (and waiting) tasks over 1, 5 and 15 minutes. Compare it with the number of CPU cores.%Cpu(s):ususer,sykernel,ninice,ididle,wawaiting for I/O,hi/siinterrupts,ststolen by the hypervisor.MiB Mem:free,used,buff/cache. The kernel uses spare memory for caching, so a smallfreeis normal. Look atavail Mem.- Per process:
PID,USER,PRpriority,NInice value,VIRT,RES(resident) andSHRmemory,Sstate,%CPU,%MEM,TIME+,COMMAND.
Interactive keys: q quit, h help, P sort by CPU, M by memory, T by time, k kill (asks for PID and signal), r renice, 1 per-CPU lines, u filter by user, c toggle command line, H threads, d change interval, W save settings.
Returns: 0 on normal exit; non-zero on bad options.
Usage notes
%CPUcan exceed 100 on multi-core machines: 100 means one full core. PressIto divide by the number of cores (Irix/Solaris mode).- The first update is misleading for CPU: it reports the average since boot. Use
-n 2and read the second block in scripts. - Memory columns:
VIRTis address space (often huge),RESis what is actually in RAM. Judge a process byRES, and the machine byavail Mem. - High
wameans the CPU waits for disk; look atvmstatandiostat. Highston a VM means a noisy neighbour. - Saving a view:
Wwrites~/.config/procps/toprc, so your sort order and columns return next time. - Logs:
top -b -n 1 -o %MEM | head -20in a cron job gives a cheap record of the biggest memory users.
Related
ps,kill,niceandrenice,free,uptime- Lessons: Processes, Performance and tricks