How to Use the top and htop Commands for System Monitoring on Linux

When a Linux server experiences a sudden spike in CPU usage or runs out of RAM, administrators cannot rely on graphical tools like the Windows Task Manager. Instead, they must turn to the terminal to diagnose the problem. The most fundamental tool for this job is top, but modern administrators heavily prefer its upgraded, visually intuitive successor: htop.

In this guide, you will learn how to read the output of these tools, understand system load averages, and quickly kill runaway processes from the command line.

The Legacy Tool: Understanding ‘top’

The top command is installed by default on virtually every Unix and Linux system in existence. It provides a real-time, dynamic view of the running system.

To run it, simply type:

top

The interface is divided into two sections:

  • The Summary Area (Top half): This displays the system uptime, the number of logged-in users, overall CPU and Memory usage, and the Load Average.
  • The Process List (Bottom half): A constantly updating list of running processes, sorted by default by CPU usage (%CPU).

To exit top, press the q key.

Understanding Load Averages

In the top right corner of the summary area, you will see “load average” followed by three numbers (e.g., 0.45, 0.60, 0.72). These represent the system load over the last 1, 5, and 15 minutes.

A load average of 1.0 on a single-core CPU means the processor is exactly at 100% capacity. If you have a 4-core server, a load average of 4.0 means 100% capacity. If the number exceeds your core count (e.g., a load of 6.0 on a 4-core machine), it means processes are waiting in a queue, and your server is heavily congested.

The Modern Upgrade: Installing and Using ‘htop’

While top is ubiquitous, it is monochromatic, difficult to scroll, and unintuitive. htop solves all these problems by adding colour coding, vertical and horizontal scrolling, and interactive mouse support.

Installing htop

htop is rarely installed by default, but it is available in all major package managers.

  • Ubuntu/Debian: sudo apt install htop
  • CentOS/RHEL: sudo yum install htop

Reading the htop Interface

Launch it by typing htop.

The most immediate improvement is the visual bar charts at the top left. You will see a numbered bar for every CPU core in your system. The colours are highly specific:

  • Green: Normal user processes (applications you are running).
  • Red: Kernel processes (the core operating system doing heavy lifting).
  • Blue: Low-priority processes (tasks that have been “niced” to run in the background).

Similarly, the Memory (Mem) bar is colour-coded: Green is used RAM, Blue is buffers, and Yellow/Orange is cached memory.

How to Manage Processes in htop

1. Sorting the List

By default, htop sorts by CPU usage. If your server is out of memory, you need to sort by RAM consumption.

  • Press F6 to open the Sort menu on the left side of the screen.
  • Use the arrow keys to select PERCENT_MEM and press Enter.

2. Filtering for Specific Apps

If you are looking for a specific process (like the Apache web server):

  • Press F4 to open the Filter bar at the bottom.
  • Type apache2 and press Enter. The list will now only show Apache processes.

3. Killing a Runaway Process

This is where htop truly shines over its predecessor. If a script has hung and is consuming 100% of a CPU core, you can terminate it instantly without needing to remember its Process ID (PID).

  1. Use the Up/Down arrow keys to highlight the offending process in the list.
  2. Press F9 to open the Kill menu.
  3. A menu will appear on the left listing various “Signals”. The default is SIGTERM (15), which asks the program to shut down gracefully. Press Enter to send it.
  4. If the program refuses to close, press F9 again, select SIGKILL (9), and press Enter. This forces the Linux kernel to terminate the process immediately, regardless of what it is doing.

By installing and mastering htop, you replace complex, arcane terminal commands with a fast, visual, and highly interactive system monitor.

Related posts

  1. How to Search Inside Files Using the grep Command in Linux
  2. How to Sort Data in Linux Using the sort Command
  3. How to Find the Size of a Directory in Linux Using the du Command

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.