Watching live load with top and htop
Video 122 of 140 in Virtualization Foundations, in the unit “Processes, signals, /proc and /sys”. 3:38, with English and Hindi audio.
A list printed once can miss a problem that happens right now. Two live views refresh the picture as the machine runs: top, which is on every Linux system, and htop, a friendlier screen that you install with apt. This article shows how to open each one, sort the busy processes to the top, read the CPU and load lines in the header, and use batch mode when you need one picture for a script or a log. It also warns about the keys that can end a process by accident.
What you will learn
- top and htop redraw the process list every few seconds, so you can watch a load change.
- Press P to sort by CPU, M to sort by memory, and q to quit top.
- The CPU line shows where the time goes: us is user programs, sy is the kernel, id is idle, and wa is waiting for disk.
- Batch mode prints one picture and stops, so it fits in a script or a log.
- Read every prompt before you act, because a stray key can open a kill menu.
Watching a load change with top
Imagine a warehouse server that is slow at this very moment. A single list printed now shows one frozen picture, and by the time it is printed, the busy job may have finished. A live view keeps redrawing the picture every few seconds, so you see which program climbs, and which one drops back.
The top command is a full-screen view that refreshes itself. It lists the busiest processes at the top, and it keeps updating while you watch. Each refresh, top reads the counters the kernel keeps for each process, compares them with the last read, and redraws the rows so the busiest ones rise to the top. Nothing on the machine is changed while you only watch.
topNote: Press P to sort by CPU, or M to sort by memory. Press q when you are finished.
Batch mode, and the htop screen
To print one picture and stop, use batch mode. That form works in a script, in a log, and over an SSH session, so it suits a machine that is already in trouble.
top -b -n 1 | head -n 12htop is not always installed on a fresh Ubuntu server, so you install it with apt. It is a friendlier screen with colours and a bar for each CPU. Its function keys are shown at the bottom.
sudo apt install htophtopReading the header and the keys
- CPU line: us is user programs, sy is the kernel, id is idle, and wa is waiting for disk. A high wa with a busy disk often explains a slow server.
- Load line: the average number of tasks running or waiting, over one, five and fifteen minutes.
- k in top: asks for a process ID and a signal, so you can end a process from inside the screen.
- F9 in htop: opens the kill menu for the selected process, and asks which signal to send.
What goes wrong with live views
A busy process is not always the bad one. A backup or a report may be working exactly as planned, so find out why before you stop it. A stray key can open a kill prompt, so read what the prompt says before you press Enter.
The live view itself uses a little CPU. On a machine already in trouble, batch mode gives you a lighter picture.
Try it yourself
You need a Linux terminal. The apt step installs software and changes the machine, so run it only on a server you are allowed to change. Press q to leave each live screen.
Open the live screen. Press P to sort by CPU, and q to quit.
topWhat you should see: The screen refreshes every few seconds, with the busiest processes at the top.
Print one picture of the top processes, and then the command stops.
top -b -n 1 | head -n 12What you should see: One picture appears, with no live refresh, and the command returns to the prompt.
Install htop, which is not always on a fresh Ubuntu server.
sudo apt install htopOpen the htop screen, with colours and a bar for each CPU.
htopWhat you should see: A friendlier screen appears, with its function keys shown at the bottom.
Terms
- Load average
- The average number of tasks that are running or waiting, shown over one, five and fifteen minutes.
- Batch mode
- A way to run top that prints one picture and stops, so it can be used in a script or a log.
Check yourself
What does a high wa figure on the CPU line point to?
Time spent waiting for a disk. With a busy disk, it often explains a slow server.
When is batch mode more useful than the live screen?
When you need one picture for a script or a log, or when the machine is already busy and the live view would add load.
Why should you not stop a busy process at once?
A busy process may be a backup or a report working as planned, so find out why before you stop it.
Previous: Reading the process list with ps. Next: What each signal means: TERM, KILL, HUP, STOP and CONT.