Uncover the infinite in IT

Table of Contents
< All Topics

Monitoring System Resources with top, htop, and vmstat

Tutorial Overview

This tutorial covers real-time monitoring of system resources using top, htop, and vmstat on a Debian-based Linux system. These tools help monitor CPU, memory, and process usage, providing valuable insights into system performance.

Prerequisites

  • Root or sudo access.

Steps

Step 1: Monitor System Resources with top

1. Open a terminal and run:

top
  • The top command provides a real-time view of system processes and resource usage.
  • Key columns include:
    • PID: Process ID.
    • USER: Owner of the process.
    • %CPU: CPU usage percentage.
    • %MEM: Memory usage percentage.
    • TIME+: Total CPU time the task has used since it started.

2. Basic Navigation in top:

  • Press M to sort by memory usage.
  • Press P to sort by CPU usage.
  • Press K to kill a process by entering its PID.
  • Press Q to quit top.

Step 2: Install and Use htop for Enhanced Monitoring

1. Install htop:

sudo apt update
sudo apt install htop -y

2. Launch htop:

htop
  • htop offers a more user-friendly and interactive interface compared to top, with color-coded bars and mouse support.

3. Navigate and Manage Processes in htop:

  • Use the arrow keys to scroll through processes.
  • Press F6 to change sorting criteria.
  • Press F9 to kill a selected process, then choose the signal type.
  • Press F10 to quit htop.

Step 3: Monitor CPU, Memory, and Disk I/O with vmstat

1. Run vmstat to see an overview of system performance:

vmstat 5
  • This command updates every 5 seconds.
  • Key columns include:
    • r: Number of processes waiting for CPU.
    • b: Number of processes in uninterruptible sleep.
    • swpd: Amount of virtual memory used.
    • free: Free memory.
    • si/so: Swap memory in/out.
    • us/sy/id/wa: CPU utilization (user, system, idle, and I/O wait).

2. Capture Historical Data with vmstat:

  • To capture data over a period, use:
vmstat 2 10
  • This command takes snapshots every 2 seconds, 10 times in total, providing a longer view of system performance.