Catalog / Process Monitoring with `top` and `htop`

Process Monitoring with `top` and `htop`

A cheat sheet for monitoring system processes using `top` and `htop` utilities, covering essential commands and options for effective system administration.

Basic `top` Usage

Overview

top provides a dynamic real-time view of running processes. It displays CPU usage, memory consumption, and other process-related information.

To start top, simply type top in the terminal.

top

Press q to exit top.

Interactive Commands

h

Display help summary.

k

Kill a process. You’ll be prompted for the PID and signal (default is 15, SIGTERM).

i

Toggle display of idle processes. Shows/hides processes that aren’t using CPU.

1

Show CPU usage per core (multiple CPUs).

M

Sort processes by memory usage.

P

Sort processes by CPU usage (default).

Command-line Options

top -d <seconds>

Specify delay between updates in seconds.

top -n <iterations>

Specify the number of iterations before top exits.

top -u <username>

Show only processes for the specified username.

top -p <pid>

Monitor only the specified process ID.

Advanced `top` Features

Field Descriptions

PID: Process ID.
USER: Username of the process owner.
PR: Priority of the process.
NI: Nice value (lower is higher priority).
VIRT: Virtual memory used by the process.
RES: Resident memory used by the process.
SHR: Shared memory used by the process.
S: Process status (e.g., S=sleeping, R=running, Z=zombie).
%CPU: CPU usage.
%MEM: Memory usage.
TIME+: CPU time used by the process.
COMMAND: Command name.

Customizing the Display

f

Field Management: Add/remove/reorder fields displayed.

o

Change the order of the displayed fields. (Not available on all versions.)

W

Write current configuration to ~/.toprc.

Understanding Load Averages

The load average (displayed at the top) shows the average number of processes that are either runnable or uninterruptible.
It’s shown for the last 1, 5, and 15 minutes.

A load average of 1.0 means the system is fully utilized by one process per core. Higher values indicate overload.

Basic `htop` Usage

Introduction to `htop`

htop is an interactive process viewer. It’s similar to top but provides a more user-friendly interface with color and improved process management features.

If htop is not installed, you can usually install it with your system’s package manager. For example, on Debian/Ubuntu:

sudo apt-get install htop

To start htop, simply type htop in the terminal.

htop

Press q or F10 to exit htop.

Keybindings

F1 or ?

Help screen.

F2

Setup: Configure htop (color, display options, etc.).

F3

Search for a process by name.

F4

Filter processes.

F5

Tree view (shows parent-child relationships between processes).

F6

Sort processes by a column (CPU, MEM, etc.).

F7 / F8

Change process priority (nice value).

F9

Kill a process.

Understanding `htop` Display

htop displays CPU usage per core, memory usage, and swap usage in a graphical format at the top.

Processes are listed with color-coded information, making it easier to identify resource-intensive processes.

Advanced `htop` Configuration

Customizing `htop`

Press F2 to enter the setup menu. You can customize various aspects of htop:

  • Columns: Add, remove, and reorder columns displayed.
  • Meters: Configure the meters displayed at the top.
  • Display options: Customize the appearance and behavior of htop.

Filtering Processes

Press F4 to filter processes by name or other criteria. This is useful for focusing on specific processes.

Enter a string to filter processes whose command lines contain that string.

Tree View

Press F5 to toggle tree view. This shows processes in a hierarchical structure, making it easier to understand parent-child relationships.

This view is helpful for identifying processes spawned by other processes.