❌

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

Learning Notes #79 – Understanding the top Command in Linux

17 February 2025 at 17:17

The top command in Linux is a powerful utility that provides realtime information about system performance, including CPU usage, memory usage, running processes, and more.

It is an essential tool for system administrators to monitor system health and manage resources effectively.

1. Basic Usage

Simply running top without any arguments displays an interactive screen showing system statistics and a list of running processes:

$ top

2. Understanding the top Output

The top interface is divided into multiple sections

Header Section

This section provides an overview of the system status, including uptime, load averages, and system resource usage.

  • Uptime and Load Average – Displays how long the system has been running and the average system load over the last 1, 5, and 15 minutes.
  • Task Summary – Shows the number of processes in various states:
    • Running – Processes actively executing on the CPU.
    • Sleeping – Processes waiting for an event or resource.
    • Stopped – Processes that have been paused.
    • Zombie – Processes that have completed execution but still have an entry in the process table. These occur when the parent process has not yet read the exit status of the child process. Zombie processes do not consume system resources but can clutter the process table if not handled properly.
  • CPU Usage – Breaks down CPU utilization into different categories:
    • us (User Space) – CPU time spent on user processes.
    • sy (System Space) – CPU time spent on kernel operations.
    • id (Idle) – Time when the CPU is not being used.
    • wa (I/O Wait) – Time spent waiting for I/O operations to complete.
    • st (Steal Time) – CPU cycles stolen by a hypervisor in a virtualized environment.
  • Memory Usage – Shows the total, used, free, and available RAM.
  • Swap Usage – Displays total, used, and free swap memory, which is used when RAM is full.

Process Table

The table below the header lists active processes with details such as:

  • PID – Process ID, a unique identifier for each process.
  • USER – The owner of the process.
  • PR – Priority of the process, affecting its scheduling.
  • NI – Nice value, which determines how favorable the process scheduling is.
  • VIRT – The total virtual memory used by the process.
  • RES – The actual RAM used by the process.
  • SHR – The shared memory portion.
  • S – Process state:
    • R – Running
    • S – Sleeping
    • Z – Zombie
    • T – Stopped
  • %CPU – The percentage of CPU time used.
  • %MEM – The percentage of RAM used.
  • TIME+ – The total CPU time consumed by the process.
  • COMMAND – The command that started the process.

3. Interactive Commands

While running top, various keyboard shortcuts allow dynamic interaction:

  • q – Quit top.
  • h – Display help.
  • k – Kill a process by entering its PID.
  • r – Renice a process (change priority).
  • z – Toggle color/monochrome mode.
  • M – Sort by memory usage.
  • P – Sort by CPU usage.
  • T – Sort by process runtime.
  • 1 – Toggle CPU usage breakdown for multi-core systems.
  • u – Filter processes by a specific user.
  • s – Change update interval.

4. Command-Line Options

The top command supports various options for customization:

  • -b (Batch mode): Used for scripting to display output in a non-interactive mode.$ top -b -n 1-n specifies the number of iterations before exit.
  • -o FIELD (Sort by a specific field):$ top -o %CPUSorts by CPU usage.
  • -d SECONDS (Refresh interval):$ top -d 3Updates the display every 3 seconds.
  • -u USERNAME (Show processes for a specific user):$ top -u john
  • -p PID (Monitor a specific process):$ top -p 1234

5. Customizing top Display

Persistent Customization

To save custom settings, press W while running top. This saves the configuration to ~/.toprc.

Changing Column Layout

  • Press f to toggle the fields displayed.
  • Press o to change sorting order.
  • Press X to highlight sorted columns.

6. Alternative to top: htop, btop

For a more user-friendly experience, htop is an alternative:

$ sudo apt install htop  # Debian-based
$ sudo yum install htop  # RHEL-based
$ htop

It provides a visually rich interface with color coding and easy navigation.

❌
❌