The top command
The top command shows a live view of how busy the machine is…
The top command shows a live view of how busy the machine is: memory use and the processes using the most CPU, redrawn every few seconds until you press q.
Taught in Ninja, lesson 53
A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.
Usage
top top -b -n 1
Show the busiest processes. Shows a summary of the machine (uptime, load average, tasks, CPU and memory) and the processes using the most CPU. Press q to quit. A process near 100% CPU is a likely culprit when a machine is slow; note its PID and look closer with ps or lsof. It fills the terminal and redraws every 3 seconds; top -b -n 1 prints it once instead, which also works in a pipe or a script.
In the practice terminal:top shows the practice machine’s pretend processes. In the terminal it fills the screen and redraws every 3 seconds, as the real one does, until you press q.
How to use top
Watch what is running
The first lines are a summary: uptime and load average, the number of tasks, CPU and memory. The busiest processes come first below. Press q to quit.
toptop - 09:01:00 up 3 days, 2:49, 1 user, load average: 0.03, 0.02, 0.02
Tasks: 5 total, 1 running, 4 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.8 us, 0.2 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 3923.4 total, 2987.1 free, 442.9 used, 493.4 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 3363.3 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
5000 learner 20 0 19610 4300 1433 S 0.3 0.1 0:00.00 top
302 root 20 0 52280 16400 5467 S 0.0 0.4 0:53.86 systemd-journal
1 root 20 0 42830 12900 4300 S 0.0 0.3 0:53.86 systemd
4242 learner 20 0 22040 5200 1733 S 0.0 0.1 0:00.01 bash
611 root 20 0 15290 2700 900 S 0.0 0.1 0:53.86 cron
Print it once: top -b -n 1
Batch mode prints a single screen and ends, so it works in a pipe or a script.
top -b -n 1 | head -12top - 09:01:00 up 3 days, 2:49, 1 user, load average: 0.03, 0.02, 0.02
Tasks: 4 total, 1 running, 3 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.6 us, 0.2 sy, 0.0 ni, 99.2 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 3923.4 total, 2991.3 free, 438.7 used, 493.4 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 3367.5 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
302 root 20 0 52280 16400 5467 S 0.0 0.4 0:53.86 systemd-journal
1 root 20 0 42830 12900 4300 S 0.0 0.3 0:53.86 systemd
4242 learner 20 0 22040 5200 1733 S 0.0 0.1 0:00.01 bash
611 root 20 0 15290 2700 900 S 0.0 0.1 0:53.86 cron
Read the load average
The three numbers are how many processes wanted the CPU, on average, over the last 1, 5 and 15 minutes. On a machine with 2 CPUs, a load near 2 means it is busy all the time.
uptime09:01:00 up 3 days, 2:49, 1 user, load average: 0.03, 0.02, 0.02
Find what is slowing a machine down
A process near 100 in the %CPU column is the usual suspect. Note its PID, look closer with ps, and stop it with kill if it shouldn’t be running.
ps -o pid,user,%cpu,cmd --sort=-%cpuPID USER %CPU CMD 4242 learner 0.0 -bash 5000 learner 0.0 ps -o pid,user,%cpu,cmd --sort=-%cpu
Options
| -b | batch mode: print instead of taking over the screen |
|---|---|
| -n N | how many snapshots to print |
Questions about top
How do I quit top?
Press q. Ctrl+C works too.
What does load average mean in top?
The average number of processes running or waiting for the CPU over the last 1, 5 and 15 minutes. Compare it with the number of CPUs: below it the machine keeps up, above it work is queuing.
What is the difference between top and htop?
htop is a friendlier top, with coloured bars, scrolling and mouse support. It usually has to be installed first: sudo apt install htop.
What do VIRT, RES and SHR mean in top?
Memory in kilobytes: VIRT is everything the process has reserved, RES what it really uses in RAM now, and SHR the part shared with other processes. RES is the one to watch.
Learn it step by step
Related commands
Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.