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

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.

Open the live screen of processes
top

Note: 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.

Print one picture of the top processes, then stop
top -b -n 1 | head -n 12

htop 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.

Install htop from the Ubuntu package list
sudo apt install htop
Open the htop screen
htop

Reading the header and the keys

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.

  1. Open the live screen. Press P to sort by CPU, and q to quit.

    top

    What you should see: The screen refreshes every few seconds, with the busiest processes at the top.

  2. Print one picture of the top processes, and then the command stops.

    top -b -n 1 | head -n 12

    What you should see: One picture appears, with no live refresh, and the command returns to the prompt.

  3. Install htop, which is not always on a fresh Ubuntu server.

    sudo apt install htop
  4. Open the htop screen, with colours and a bar for each CPU.

    htop

    What 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.