htop Command: Tutorial & Examples

Monitoring System Resources Interactively

The htop command is an interactive process viewer and system monitor for Linux systems. It provides a real-time, colorful, and user-friendly interface to display system metrics such as CPU usage, memory consumption, load averages, and detailed information about running processes. Unlike the traditional top command, htop offers enhanced functionality like vertical and horizontal scrolling, mouse support, and easier process management.

This command is widely used by system administrators and users to monitor system performance, diagnose high load conditions, and manage processes effectively. Its intuitive interface simplifies tasks such as sorting, filtering, and killing processes without requiring memorization of process IDs.

Technical Background

htop obtains its information primarily from the /proc virtual filesystem, which exposes kernel and process data to user space. It periodically reads this data to present an updated view of system resources and process statuses. The interactive interface is implemented using ncurses library functionality, enabling colored output and keyboard/mouse navigation within a terminal.

Why htop Is Important

htop enhances system monitoring and process management by providing:

  • Complete Process Listing: Displays all running processes, not only the highest resource consumers.
  • Scrolling Capability: Allows vertical and horizontal scrolling to view all processes and full command lines.
  • Interactive Process Management: Enables selecting processes via keyboard or mouse and sending signals (e.g., to terminate processes) without needing to know their PIDs.
  • Color-Coded Metrics: Uses colors to visually differentiate resource usage and process states.
  • Customizable Display: Supports multiple sorting criteria and filtering options for tailored monitoring.
  • Resource Overview: Provides concise, real-time summaries of CPU, memory, and swap usage.
  • User-Friendly Navigation: Supports mouse input and function keys for quick access to commands.

These features make htop a valuable tool for diagnosing performance issues, identifying resource-heavy processes, and maintaining system health.

Common Command-Line Parameters

htop supports various command-line options to customize its behavior. Some commonly used parameters include:

  • -u username

    htop -u username
    

Displays only the processes owned by the specified user.

  • -p pid[,pid,...]

    htop -p 123,456,789
    

Monitors only the processes with the specified process IDs.

  • -d delay

    htop -d 20
    

Sets the delay between updates to the specified tenths of seconds (default is 100, i.e., 10 seconds).

  • -s column

    htop -s PERCENT_MEM
    

Sorts the process list by the given column (e.g., CPU%, MEM%, TIME, PID, etc.).

  • -C

    htop -C
    

Starts htop in monochrome (no color) mode.

  • --no-color

    htop --no-color
    

Disables color output.

  • --version

    htop --version
    

Displays the version information.

Examples of Using htop

To launch htop with default settings, simply run:

    htop

Sample output will show colored CPU bars, memory usage, and a list of all processes.

To monitor processes of a specific user only:

    htop -u alice

To track specific processes by their PIDs (e.g., 1010 and 2020):

    htop -p 1010,2020

To change the update interval to 5 seconds (50 tenths):

    htop -d 50

To start htop sorted by memory usage in descending order:

    htop -s PERCENT_MEM

Within the interactive interface, you can:

  • Use arrow keys or mouse scroll to navigate the process list vertically.
  • Use left/right arrow keys to scroll horizontally and view full command lines.
  • Press F6 to change the sorting column.
  • Press F9 to kill a selected process (select signal type).
  • Press F3 to search for processes by name.
  • Press F4 to filter processes by a string.
  • Press F10 or q to exit htop.

Example: Killing a process

  1. Start htop.
  2. Navigate to the target process using arrow keys or mouse.
  3. Press F9.
  4. Select a signal (e.g., SIGTERM, SIGKILL) and press Enter.

Interpreting htop Output

The htop display is divided into three main sections:

  • Header Section

    Displays system-wide metrics:

    • CPU meters: Colored bars showing utilization per CPU core.
    • Tasks: Total number of processes, threads, and how many are running.
    • Load average: System load averages for the last 1, 5, and 15 minutes.
    • Memory and Swap: Used and total amounts of RAM and swap space.
    • Uptime: How long the system has been running.
  • Process List Section

    A detailed list of running processes, each line showing columns such as:

    • PID: Process ID.
    • USER: Owner of the process.
    • PRI: Priority.
    • NI: Nice value.
    • VIRT: Virtual memory size.
    • RES: Resident memory size.
    • SHR: Shared memory.
    • S: Process state (e.g., R for running, S for sleeping).
    • CPU%: Percentage of CPU usage.
    • MEM%: Percentage of memory usage.
    • TIME+: Total CPU time consumed.
    • Command: Command name or full command line.
  • Footer Menu

    Shows function key shortcuts for actions like:

    • F1: Help
    • F2: Setup (configuration)
    • F3: Search
    • F4: Filter
    • F5: Tree view toggle
    • F6: Sort options
    • F7/F8: Nice value adjustment
    • F9: Kill process
    • F10: Quit

A typical output snippet might look like this:

    1  [|||||||                                  17.2%]     Tasks: 33, 57 thr; 2 running
    2  [||||||||                                 20.2%]     Load average: 0.08 0.02 0.01 
    Mem[|||||||||||||||||||||||||||||       972M/3.86G]     Uptime: 00:11:22
    Swp[                                         0K/0K]

    PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command            
    1084 alan       20   0  657M  5372  3848 R  6.2  0.1  0:00.18 htop

Potential Problems and Troubleshooting

  • htop command not found

    If htop is not installed, you will see:

    bash: htop: command not found
    

Install it using your package manager, for example:

    sudo apt-get install htop       # Debian/Ubuntu
    sudo yum install htop           # CentOS/RHEL
    sudo dnf install htop           # Fedora
    sudo pacman -S htop             # Arch Linux
  • Permission Issues

    Monitoring processes owned by other users may require superuser privileges. Run sudo htop to view all processes.

  • Display Problems

    If colors or interface elements do not render correctly, ensure your terminal supports ANSI color codes and that htop is not running in monochrome mode.

  • High CPU Usage by htop

    Running htop with very short update intervals or monitoring many processes on low-powered systems may cause increased CPU usage by htop itself.

Customization and Configuration

htop allows customization via its interactive setup menu (press F2):

  • Meters: Add, remove, or reposition CPU, memory, and swap meters in the header.
  • Columns: Choose which columns to display in the process list and their order.
  • Colors: Adjust color schemes for better readability.
  • Display Options: Toggle tree view, hide kernel threads, or display user threads.
  • Sorting: Set the default sort column and order.
  • Mouse Support: Enable or disable mouse interaction.

Configuration changes are saved in the user’s home directory under:

    ~/.config/htop/htoprc

You can edit this file manually to apply advanced customizations.

Scripting and Automation

While htop is primarily an interactive tool, it can be used in scripts by combining it with other commands:

  • Use htop in batch mode (not officially supported) by capturing snapshots of the interface.
  • For scripting process monitoring, consider using commands like ps, top with batch options, or pidstat.

htop is ideal for manual inspection but less suited for automated logging or alerting.

Performance and Security Considerations

  • Running htop typically consumes minimal system resources, but on systems with thousands of processes, it may add some load.
  • Running as root (sudo htop) allows viewing all processes but also increases security risks if the terminal session is compromised.
  • Avoid running htop on untrusted terminals or over insecure connections without encryption.
  • htop does not modify system state unless used to send signals to processes, so its security impact is limited to process management.

See Also

Further Reading

As an Amazon Associate, I earn from qualifying purchases.

The text above is licensed under CC BY-SA 4.0 CC BY SA