Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

System Monitoring: Using top and htop

Introduction

Monitoring system performance is crucial for maintaining a healthy and efficient computing environment. Two commonly used command-line tools for this purpose are top and htop. These tools provide real-time information about system processes, memory usage, and CPU load, among other things. This tutorial will walk you through the basics of using top and htop with detailed explanations and examples.

Using top

The top command is a standard tool that comes pre-installed on most Unix-like operating systems. It provides a dynamic, real-time view of the system, including the tasks managed by the kernel.

Basic Usage

To start top, simply open your terminal and type:

top

This will bring up an interface that looks something like this:

top - 15:45:18 up 1:23, 2 users, load average: 0.00, 0.01, 0.05
Tasks: 98 total, 1 running, 97 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.1 sy, 0.0 ni, 99.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 2048000 total, 1024000 free, 512000 used, 512000 buff/cache
KiB Swap: 2048000 total, 2048000 free, 0 used. 1024000 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 root 20 0 12345 6789 1234 S 0.3 0.3 0:00.23 top
...

Key Metrics

The top interface displays a wealth of information, including:

  • System Uptime: How long the system has been running.
  • Number of Users: How many users are currently logged in.
  • Load Average: The system load over the last 1, 5, and 15 minutes.
  • Tasks: The number of running, sleeping, stopped, and zombie processes.
  • CPU Usage: The percentage of CPU time spent in various states (user, system, idle, etc.).
  • Memory Usage: Information about physical memory and swap usage.
  • Process List: A list of processes, including their PID, user, priority, memory usage, CPU usage, and the command being executed.

Interactive Commands

While top is running, you can interact with it using various keyboard commands:

  • h: Display help.
  • q: Quit top.
  • k: Kill a process. You will be prompted to enter the PID.
  • r: Renice a process. You will be prompted to enter the PID and the new priority.
  • Space: Refresh the display.

Using htop

The htop command is an interactive process viewer for Unix systems. It is a more user-friendly and visually appealing alternative to top. Unlike top, htop provides a full-featured, interactive interface and can be customized to suit your needs.

Installation

On most systems, htop is not installed by default. You can install it using your package manager. For example:

sudo apt-get install htop

sudo yum install htop

Basic Usage

To start htop, simply open your terminal and type:

htop

This will bring up an interface that looks something like this:

  1  [|||                                                2.0%]   Tasks: 43, 55 thr; 1 running
  2  [|||                                                2.0%]   Load average: 0.04 0.09 0.09 
  Mem[||||||||||                                     187/2048MB]   Uptime: 00:23:45
  Swp[                                              0/2048MB]

    PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
   1234 root       20   0  123M 6789K 1234K S  0.3  0.3   0:00.23 htop
   ...

Key Metrics

The htop interface displays similar information to top, but in a more user-friendly format:

  • CPU Usage: Graphical representation of CPU usage for each core.
  • Memory Usage: Graphical representation of memory and swap usage.
  • Load Average: The system load over the last 1, 5, and 15 minutes.
  • Uptime: How long the system has been running.
  • Process List: A list of processes, including their PID, user, priority, memory usage, CPU usage, and the command being executed.

Interactive Commands

While htop is running, you can interact with it using various keyboard commands:

  • F1: Display help.
  • F2: Setup (customize htop).
  • F3: Search for a process.
  • F4: Filter processes.
  • F5: Tree view.
  • F6: Sort by a column.
  • F9: Kill a process.
  • F10: Quit htop.
  • Space: Tag a process.

Conclusion

Both top and htop are powerful tools for monitoring system performance and managing processes. While top is widely available and provides essential information, htop offers a more intuitive and customizable interface. Understanding how to use these tools effectively can help you maintain the health and efficiency of your system.