Linux htop command options


The htop command in Linux is a powerful, interactive tool for monitoring system performance, including CPU usage, memory usage, and process management. It comes with several useful options that allow you to customize its behavior and output. Below, I’ll explain the most common htop command options with examples.

Basic Syntax:

htop [options]

1. -d <delay>

  • Purpose: Set the delay (in milliseconds) between updates.
  • Example:
    htop -d 500
    This command sets the delay between screen updates to 500 milliseconds. By default, htop updates every second.

2. -u <user>

  • Purpose: Display processes for a specific user.
  • Example:
    htop -u john
    This will display only the processes that belong to the user "john."

3. -p <pid>

  • Purpose: Display information for a specific process ID (PID).
  • Example:
    htop -p 1234
    This command will show information only for the process with PID 1234.

4. -C

  • Purpose: Disable color output (useful for viewing output in a monochrome terminal or when logging to a file).
  • Example:
    htop -C
    This disables color, making the output plain text, which can be useful for scripts or logging.

5. -b

  • Purpose: Start htop in batch mode. This mode is non-interactive and is useful for logging or redirecting the output to a file.
  • Example:
    htop -b -n 1 > htop_output.txt
    This runs htop in batch mode for one iteration and redirects the output to htop_output.txt. The -n 1 option ensures that htop runs for only one iteration before exiting.

6. -s <field>

  • Purpose: Sort processes by a specific field.
  • Example:
    htop -s %CPU
    This will sort the processes based on CPU usage, displaying the highest CPU consumers at the top. You can also sort by other fields like %MEM for memory usage, PID for process ID, etc.

7. -S

  • Purpose: Display cumulative CPU time for processes, rather than the current CPU usage.
  • Example:
    htop -S
    This will display processes with cumulative CPU time, which gives you a sense of the total CPU time each process has consumed since it started.

8. -n <iterations>

  • Purpose: Set the number of iterations htop should run before exiting.
  • Example:
    htop -n 3
    This runs htop for 3 iterations and then exits. It's useful if you want a snapshot of the system for a set number of updates.

9. -s <column_name>

  • Purpose: Sort processes by a specific column name.
  • Example:
    htop -s "MEM"
    This will sort the processes by memory usage in descending order. You can also use %CPU, TIME+, or PID to sort by those columns.

10. -h

  • Purpose: Display help (this is the default when you run htop without any options).
  • Example:
    htop -h
    This shows help and a list of all the available commands and keybindings for interacting with htop.

11. -v

  • Purpose: Display version information.
  • Example:
    htop -v
    This will display the version of htop installed on your system.

Example Usage:

  1. Running htop with a custom delay:

    htop -d 1000

    This will update the htop display every second (1000 milliseconds).

  2. Display processes only for a specific user:

    htop -u root

    This will show only processes running under the "root" user.

  3. Run htop in batch mode and save the output:

    htop -b -n 5 > htop_output.txt

    This will run htop for 5 iterations in batch mode and save the output to htop_output.txt.

  4. Sort processes by CPU usage:

    htop -s %CPU

    This sorts processes by CPU usage, showing the processes with the highest CPU usage at the top.

  5. Run htop for a specific process by PID:

    htop -p 12345

    This will display detailed information about the process with PID 12345.

Interactive Features in htop:

While htop is running, you can use interactive commands to interact with processes and adjust the display:

  • F1: Show help
  • F2: Setup (for customizing the appearance of htop)
  • F3: Search for a process
  • F4: Filter processes
  • F5: Display processes in a tree view
  • F6: Sort processes by a selected column
  • F7: Decrease process priority (renice)
  • F8: Increase process priority (renice)
  • F9: Kill a process
  • F10: Quit htop

Summary of Common Options:

  • -d <delay>: Set the update delay (in milliseconds).
  • -u <user>: Show processes for a specific user.
  • -p <pid>: Show details for a specific process.
  • -C: Disable color output.
  • -b: Run in batch mode (useful for logging).
  • -s <field>: Sort processes by a specific field.
  • -S: Show cumulative CPU time.
  • -n <iterations>: Set the number of iterations before exiting.
  • -v: Display version information.
  • -h: Display help.

htop provides a more user-friendly, feature-rich experience than top, with interactive features for sorting, filtering, killing, and renicing processes. You can also customize its output with various options to match your monitoring needs.