Linux htop command


htop is an interactive process viewer and system monitor for Linux, which is often considered a more user-friendly alternative to top. It provides a colorful, text-based, real-time view of system processes, CPU usage, memory consumption, and more. Unlike top, htop has a better interface for managing processes, such as the ability to kill or renice processes with just a few keystrokes.

Installation

htop might not be installed by default, but you can install it easily using your package manager.

On Debian/Ubuntu-based systems:

sudo apt-get install htop

On Red Hat/CentOS/Fedora-based systems:

sudo yum install htop

On Arch Linux:

sudo pacman -S htop

Basic Usage

To launch htop, simply type:

htop

This will open the htop interface, which is divided into several sections:

1. Header (System Overview)

At the top of the htop output, you’ll see an overview of your system:

  • CPU Usage: Displayed as a bar graph, indicating the percentage of CPU usage for each core.
  • Memory Usage: A bar graph showing the percentage of RAM being used.
  • Swap Usage: A bar graph showing the percentage of swap space being used.
  • Load Averages: This shows the system's load average over 1, 5, and 15 minutes.
  • Uptime: The system's uptime since the last reboot.

Example:

1 [||||||||||||||||||||| 85.5%] Tasks: 100 total, 2 running, 98 sleeping, 0 stopped, 0 zombie 2 [||||||| 18.5%] Load average: 0.50, 0.75, 0.80 3 [||||||| 16.7%] Uptime: 1 day, 2:10 4 [| 2.3%] Mem[| 4.6G/8.0G] Swap[| 0.0G/2.0G]

2. Process List

The main section of htop displays all currently running processes with the following columns:

ColumnDescription
PIDProcess ID
USERUser who owns the process
PRIProcess priority
NINice value (affects process priority)
VIRTVirtual memory used by the process
RESResident memory used by the process
SHRShared memory used by the process
SProcess state (e.g., S for sleeping, R for running)
%CPUPercentage of CPU usage by the process
%MEMPercentage of memory usage by the process
TIME+Total CPU time consumed by the process
COMMANDName of the command that started the process

Processes are displayed in a table, with the option to sort and filter based on columns. You can interactively sort by CPU usage, memory usage, or other metrics.

Example:

PID USER PRI NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3245 john 20 0 512000 92000 13000 S 3.5 1.2 1:23.45 firefox 2134 root 20 0 248000 45000 8000 R 7.0 0.6 0:25.67 apache2 4021 jane 20 0 85000 25000 3000 S 1.2 0.3 0:03.12 vim

3. Navigating htop

htop is interactive and has several keyboard shortcuts that allow you to manage processes and customize the display. Some common keyboard commands include:

  • F1: Show help (lists all commands).
  • F2: Setup - Allows you to customize how htop looks (change colors, columns, etc.).
  • F3: Search - Search for a process by name.
  • F4: Filter - Filter processes by a keyword.
  • F5: Tree - View processes in a tree-like structure (shows parent-child relationships).
  • F6: Sort by column - Choose a column to sort by (e.g., %CPU, %MEM).
  • F7: Decrease priority (renice) - Lower the process priority.
  • F8: Increase priority (renice) - Increase the process priority.
  • F9: Kill process - Send a signal to terminate the process.
  • F10: Quit htop - Exit the htop interface.

You can also use the arrow keys to scroll through the list of processes.

4. Customizing htop Display

You can customize htop to display more information or change the way it looks.

  • Display Columns: To add or remove columns, press F2 to go into the "Setup" menu, then navigate to "Columns" to choose the columns you want to display.
  • Sorting: You can sort processes by clicking on the column header (e.g., CPU, MEM, PID) or by using the F6 key.

5. Additional Options

You can use the following command-line options to modify how htop behaves when starting:

htop -d <delay>

  • Purpose: Set the delay between updates in milliseconds.
  • Example:
    htop -d 200
    This sets the update delay to 200 milliseconds.

htop -u <user>

  • Purpose: Show processes for a specific user.
  • Example:
    htop -u john
    This shows only the processes running for the user "john".

htop -p <pid>

  • Purpose: Display information for a specific process.
  • Example:
    htop -p 3245
    This shows information for the process with PID 3245.

htop -b

  • Purpose: Start htop in batch mode (useful for scripting or logging).
  • Example:
    htop -b -n 1
    This will run htop in batch mode for one iteration and output the results to the terminal.

Example Output of htop

1 [||||||||||||||||||||| 85.5%] Tasks: 100 total, 2 running, 98 sleeping, 0 stopped, 0 zombie 2 [||||||| 18.5%] Load average: 0.50, 0.75, 0.80 3 [||||||| 16.7%] Uptime: 1 day, 2:10 4 [| 2.3%] Mem[| 4.6G/8.0G] Swap[| 0.0G/2.0G] PID USER PRI NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3245 john 20 0 512000 92000 13000 S 3.5 1.2 1:23.45 firefox 2134 root 20 0 248000 45000 8000 R 7.0 0.6 0:25.67 apache2 4021 jane 20 0 85000 25000 3000 S 1.2 0.3 0:03.12 vim

In the above example:

  • The CPU usage for the first core is at 85.5%.
  • Memory usage is 4.6GB out of 8GB, with no swap used.
  • The process list includes processes like firefox, apache2, and vim.

Summary

htop is a much more interactive, colorful, and user-friendly tool than top. It offers real-time monitoring of system resources, an easy-to-read display, and the ability to interact with and manage processes directly from the interface. It can be customized to display various system stats and supports several interactive features like sorting, filtering, and killing processes.