# Process Monitoring and Resource Analysis with top and htop: A Practical Guide

> **Use `top` for lightweight scripted monitoring and `htop` for interactive visual troubleshooting; both utilities read from the `/proc` virtual filesystem to display real-time CPU, memory, and I/O statistics without requiring r...

- Repository: [Joshua Levy/the-art-of-command-line](https://github.com/jlevy/the-art-of-command-line)
- Tags: how-to-guide
- Published: 2026-02-24

---

**Use `top` for lightweight scripted monitoring and `htop` for interactive visual troubleshooting; both utilities read from the `/proc` virtual filesystem to display real-time CPU, memory, and I/O statistics without requiring root privileges for basic operation.**

The `the-art-of-command-line` repository documents these tools as essential utilities for diagnosing system health, referencing them in [`README.md`](https://github.com/jlevy/the-art-of-command-line/blob/main/README.md) at lines 316 and 523 as the standard solutions for checking current CPU and disk status. Understanding how to leverage both commands allows you to spot resource hogs, filter process trees, and capture performance snapshots from any Linux terminal.

## Architecture and Data Sources

Both utilities are implemented in C and rely on the **procfs** pseudo-filesystem to gather metrics. They generate output on-the-fly each refresh cycle, ensuring you always see the current system state.

### How top Accesses System Metrics

**`top`** is part of the `procps` suite and reads directly from `/proc/[pid]/stat`, `/proc/stat`, and `/proc/meminfo` every refresh interval. By default, it updates every 3 seconds (configurable with the `-d` flag). The tool uses a minimal text-based interface optimized for low-bandwidth SSH connections and operates with a very low memory footprint of just a few hundred kilobytes.

### How htop Renders Interactive Data

**`htop`** is a stand-alone project that parses the same `/proc` sources but uses **ncurses** to render a full-screen UI with color-coded bars, mouse support, and hierarchical process views. It defaults to a 1-second refresh interval and supports per-CPU metrics through a more flexible internal parsing model. While slightly heavier than `top`, it remains lightweight and offers interactive filtering via function keys like `F3` (search), `F4` (filter), and `F5` (tree view).

## Essential Command-Line Options

Mastering these flags allows you to automate monitoring and customize your view:

- **`top -b`** – Runs in batch mode, sending output to stdout for scripting instead of the interactive screen.
- **`top -d <seconds>`** – Sets the refresh delay (e.g., `top -d 1` for 1-second updates).
- **`top -n <count>`** – Limits iterations when combined with `-b`, useful for capturing snapshots.
- **`htop -d <deciseconds>`** – Sets refresh delay in tenths of seconds (e.g., `htop -d 5` equals 0.5 seconds).
- **`htop -p <pid>`** – Monitors only specific process IDs, showing them and their children.
- **`htop -u <user>`** – Filters the display to show only processes owned by the specified user.
- **`htop -s <field>`** – Sorts by column on startup (e.g., `-s MEM%` or `-s CPU%`).

Both tools can be prefixed with **sudo** to reveal processes owned by other users or to access restricted kernel metrics.

## Practical Usage Examples

### Capture System Snapshots for Logging

Run `top` in batch mode to record 10 iterations at 1-second intervals to a file:

```bash
top -b -d 1 -n 10 > /tmp/top.log

```

### Monitor a Specific User's Processes

Launch `htop` filtered to show only processes owned by `alice`:

```bash
htop -u alice

```

### Debug a Single Process Tree

Focus on a specific PID and its children using `htop`:

```bash
htop -p 1234

```

### Extract CPU Usage Percentage in Scripts

Parse `top` batch output to calculate current CPU load:

```bash
cpu=$(top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}')
echo "Current CPU usage: $cpu %"

```

### Interactive Memory Analysis

1. Start `htop`.
2. Press `F6` and select **MEM%** to sort by memory consumption.
3. Press `F4`, type `idle`, and press **Enter** to filter out idle processes.

### Generate a Static htop Report

Capture a single-screen text snapshot suitable for bug reports:

```bash
htop -b -d 5 -n 1 > /tmp/htop_snapshot.txt

```

## When to Use top vs htop

Choose your tool based on the operational context:

- **Scripting and automation** – Use **`top -b`** (batch mode) for cron jobs and automated monitoring pipelines.
- **Interactive troubleshooting** – Use **`htop`** when you need mouse support, color-coded visual cues, and the tree view to explore process hierarchies.
- **Remote low-bandwidth connections** – Use **`top`** to minimize UI overhead when connected via slow SSH links.
- **Process hierarchy analysis** – Use **`htop`** with `F5` to visualize parent-child relationships.
- **Learning Linux internals** – Start with **`top`** to understand raw procfs metrics, then graduate to **`htop`** for advanced filtering.

## Source Reference in the-art-of-command-line

According to the repository source, the primary documentation appears in [`README.md`](https://github.com/jlevy/the-art-of-command-line/blob/main/README.md) at line 316, which lists `top` and `htop` as the go-to solutions for "current cpu/disk status." A dedicated entry for `htop` appears at line 523, describing it as an improved interactive process viewer. The repository contains no executable code for these utilities; instead, it serves as a curated cheat-sheet that points system administrators toward these standard Linux tools.

## Summary

- Both `top` and `htop` read from `/proc` to display real-time system metrics without requiring privileged access for basic operation.
- **`top`** excels in batch mode (`-b`) for scripted automation and operates with minimal resource overhead.
- **`htop`** provides a superior interactive experience with mouse support, tree views, and dynamic filtering via function keys.
- Use **`top -d`** and **`htop -d`** to control refresh intervals, noting that `top` uses seconds while `htop` uses deciseconds.
- Filter processes in `htop` using the `-u` flag for users or `-p` for specific PIDs.
- Reference the `the-art-of-command-line` [`README.md`](https://github.com/jlevy/the-art-of-command-line/blob/main/README.md) (lines 316 and 523) for quick reminders on these essential commands.

## Frequently Asked Questions

### Do I need root privileges to run top or htop?

No. Both utilities read from the `/proc` virtual filesystem, which is world-readable on standard Linux systems. However, running with **sudo** allows you to see all processes (including those hidden for privacy reasons) and enables actions like killing processes owned by other users or viewing kernel-level metrics.

### Can I use top or htop in shell scripts for automated monitoring?

**`top`** is specifically designed for scripting via its batch mode (`top -b -n1`). This outputs plain text that tools like `awk` and `grep` can parse. **`htop`** is optimized for interactive use with its ncurses interface and is not suitable for standard shell scripting pipelines, though it does offer a batch mode (`-b`) primarily for generating static reports.

### How do I filter processes by user in htop?

Use the command-line flag **`htop -u username`** to launch with a pre-applied user filter. Alternatively, start `htop` interactively and press `F4` to open the filter dialog, then type the username. You can also press `u` (lowercase) in interactive mode to select a user from a list.

### What is the default refresh interval for top and htop?

**`top`** refreshes every **3 seconds** by default, configurable with the `-d` flag in whole seconds. **`htop`** refreshes every **1 second** by default, configurable with the `-d` flag in tenths of seconds (deciseconds). For example, `htop -d 10` sets a 1-second delay, equivalent to `top -d 1`.