如何在htop中按不同标准对进程进行排序

LinuxLinuxBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

Introduction

The htop utility is an enhanced version of the traditional top command in Linux systems. It provides a user-friendly, interactive interface for monitoring system resources and managing processes in real-time. This tutorial will guide you through using htop with a focus on sorting processes by different criteria, such as CPU usage, memory consumption, and process IDs.

By learning to effectively sort processes in htop, you will be able to:

  • Quickly identify resource-intensive applications
  • Monitor system performance more efficiently
  • Locate specific processes when troubleshooting
  • Better manage your system resources

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/TextProcessingGroup(["Text Processing"]) linux(("Linux")) -.-> linux/ProcessManagementandControlGroup(["Process Management and Control"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux(("Linux")) -.-> linux/PackagesandSoftwaresGroup(["Packages and Softwares"]) linux/BasicSystemCommandsGroup -.-> linux/help("Command Assistance") linux/BasicSystemCommandsGroup -.-> linux/exit("Shell Exiting") linux/TextProcessingGroup -.-> linux/sort("Text Sorting") linux/ProcessManagementandControlGroup -.-> linux/kill("Process Terminating") linux/ProcessManagementandControlGroup -.-> linux/pkill("Pattern-Based Killing") linux/SystemInformationandMonitoringGroup -.-> linux/ps("Process Displaying") linux/SystemInformationandMonitoringGroup -.-> linux/service("Service Managing") linux/PackagesandSoftwaresGroup -.-> linux/apt("Package Handling") subgraph Lab Skills linux/help -.-> lab-415299{{"如何在htop中按不同标准对进程进行排序"}} linux/exit -.-> lab-415299{{"如何在htop中按不同标准对进程进行排序"}} linux/sort -.-> lab-415299{{"如何在htop中按不同标准对进程进行排序"}} linux/kill -.-> lab-415299{{"如何在htop中按不同标准对进程进行排序"}} linux/pkill -.-> lab-415299{{"如何在htop中按不同标准对进程进行排序"}} linux/ps -.-> lab-415299{{"如何在htop中按不同标准对进程进行排序"}} linux/service -.-> lab-415299{{"如何在htop中按不同标准对进程进行排序"}} linux/apt -.-> lab-415299{{"如何在htop中按不同标准对进程进行排序"}} end

Installing and Launching htop

In this first step, we will install the htop utility on your Ubuntu system and launch it for the first time.

Installing htop

Before we can use htop, we need to install it. Open your terminal and execute the following commands:

sudo apt update
sudo apt install htop -y

The first command updates your package lists, and the second command installs the htop package. The -y flag automatically confirms the installation.

You should see output similar to the following:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  htop
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 132 kB of archives.
After this operation, 301 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy/main amd64 htop amd64 3.0.5-7build2 [132 kB]
Fetched 132 kB in 1s (132 kB/s)
Selecting previously unselected package htop.
(Reading database ...
Unpacking htop (3.0.5-7build2) ...
Setting up htop (3.0.5-7build2) ...
Processing triggers for man-db (2.10.2-1) ...

Launching htop

Now that htop is installed, you can launch it by simply typing the following command in your terminal:

htop

You should see an interactive display showing:

  • A header with CPU, memory, and swap usage bars
  • A process list showing running processes on your system
  • A footer with keyboard shortcuts
htop initial screen

Take a moment to observe the interface. The process list typically includes columns for:

  • PID (Process ID)
  • USER (Process owner)
  • PRI (Priority)
  • NI (Nice value)
  • VIRT (Virtual memory)
  • RES (Resident memory)
  • SHR (Shared memory)
  • S (Status)
  • CPU% (CPU usage percentage)
  • MEM% (Memory usage percentage)
  • TIME+ (CPU time)
  • Command (Process name/command)

To exit htop when you're done exploring, press the q key on your keyboard.

Now that you have installed and launched htop, let's explore its interface and learn how to navigate through it.

Launch htop again by typing:

htop

Using Arrow Keys

In the htop interface:

  • Use the Up and Down arrow keys to move the selection cursor through the process list
  • Use the Left and Right arrow keys to scroll horizontally if the process list is wider than your screen

Try using these arrow keys now to navigate through the process list.

Scrolling Through Pages

If you have many processes running, you can use:

  • Page Up and Page Down keys to scroll through pages of processes
  • Home key to jump to the first process
  • End key to jump to the last process

Understanding the Display

Take a moment to understand the main components of the htop display:

  1. Header: Shows system-wide information

    • CPU usage bars (one for each CPU core)
    • Memory usage bar
    • Swap usage bar
    • Uptime (how long the system has been running)
    • Load average (system load over the last 1, 5, and 15 minutes)
  2. Process List: Shows detailed information about each process

    • Each column represents a different attribute of the processes
    • The currently selected process is highlighted
  3. Footer: Shows available keyboard shortcuts

    • Function keys (F1-F10) for common actions
    • These shortcuts are essential for using htop effectively

Let's test some basic navigation. Try locating the htop process itself in the list. It should be visible among the running processes. Use the arrow keys to scroll until you find it.

To get help within htop, press the F1 key or h. This opens the help screen with a complete list of keyboard shortcuts.

To exit the help screen, press the Escape key or q.

When you're done exploring, exit htop by pressing q.

Sorting Processes in htop

The ability to sort processes based on different criteria is one of the most useful features of htop. In this step, we'll learn various ways to sort processes to easily identify resource-intensive applications or locate specific processes.

Sorting Methods in htop

Launch htop again:

htop

The simplest way to sort processes is by using the dedicated sort menu:

  1. Press F6 while in htop. This opens the sort menu.

  2. You'll see a list of columns you can sort by:

    • PID
    • USER
    • PRIORITY
    • NICE
    • M_SIZE (Memory size)
    • M_RESIDENT (Resident memory)
    • M_SHARE (Shared memory)
    • STATE
    • PERCENT_CPU
    • PERCENT_MEM
    • TIME
    • COMM (Command)
  3. Use the arrow keys to select a column, then press Enter to sort by that column.

For example, to sort by CPU usage:

  1. Press F6
  2. Select PERCENT_CPU using arrow keys
  3. Press Enter

Now the processes are sorted by CPU usage, with the highest CPU-consuming processes at the top.

Direct Sorting Shortcuts

htop also provides direct keyboard shortcuts for common sorting options:

  • Press P to sort by CPU usage (Processor)
  • Press M to sort by memory usage
  • Press T to sort by time (cumulative CPU time)
  • Press N to sort by process ID

Try each of these shortcuts now to see how the process list changes.

For example, press M to sort by memory usage. Notice how the processes are now arranged with the most memory-intensive ones at the top.

Reversing Sort Order

You can reverse the current sort order by pressing I (capital i). This toggles between ascending and descending order.

Try sorting by CPU usage with P, then press I to reverse the order. The processes with the lowest CPU usage will now appear at the top.

Column Highlighting

When you sort by a specific column, that column gets highlighted to indicate it's the current sort key. This makes it easy to remember which criterion you're currently using to sort the processes.

Try different sorting methods and observe how the highlighted column changes to reflect your selection.

Practical Example: Finding Memory-Intensive Processes

Let's practice with a specific scenario. Suppose you want to identify which processes are consuming the most memory:

  1. Press M to sort by memory usage
  2. The processes are now ordered by memory consumption (highest to lowest)
  3. The top processes in the list are your system's most memory-intensive applications

This sorting method is extremely useful when troubleshooting memory issues or identifying memory leaks.

When you're done exploring, exit htop by pressing q.

Managing Processes Based on Sorted Information

After learning how to sort processes in htop, let's explore how to use this sorted information to manage processes effectively.

Process Management Actions in htop

Launch htop once more:

htop

Selecting and Managing Processes

Once you've sorted the processes and identified ones of interest, you can perform various actions on them:

  1. Select a process: Navigate to it using the arrow keys
  2. View process details: Press F2 to customize the view or space to tag a process for batch operations
  3. Kill a process: Press F9 to send a signal to a process (such as terminating it)
  4. Change process priority: Press F7 to decrease priority (higher nice value) or F8 to increase priority (lower nice value)

Let's practice these actions:

Sorting and Examining Resource-Intensive Processes

  1. Press P to sort by CPU usage
  2. Navigate to one of the top CPU-consuming processes using arrow keys
  3. Press F9 to open the "Send signal" menu
    • Note that we won't actually send a kill signal; this is just for demonstration
    • Press Esc to cancel the operation

You can see how easy it is to identify and manage high-resource processes with the sorting feature.

Changing Process Priority

After sorting processes, you might want to adjust the priority of certain resource-intensive processes:

  1. Press M to sort by memory usage
  2. Navigate to a process with high memory usage
  3. Press F8 to open the priority adjustment menu
    • This would allow you to increase the priority
    • Press Esc to cancel without making changes

Sorting by User

You can also sort processes by user, which is useful in multi-user environments:

  1. Press F6 to open the sort menu
  2. Select USER and press Enter
  3. The processes are now grouped by user, making it easy to see which users are running which processes

Tree View with Sorting

htop provides a tree view that shows the parent-child relationships between processes:

  1. Press F5 to toggle tree view

  2. Notice how processes are organized hierarchically

  3. You can still use sorting while in tree view

    • Press P to sort by CPU usage
    • The processes still maintain their hierarchical relationships while being sorted
  4. Press F5 again to go back to the flat view

Filtering Processes

You can combine sorting with filtering to narrow down the process list:

  1. Press F4 to activate the filter

  2. Type a search term (e.g., bash)

  3. Press Enter to apply the filter

  4. Now you can sort these filtered results using any sorting method

    • Press P to sort filtered processes by CPU usage
    • Press M to sort filtered processes by memory usage
  5. To clear the filter, press F4 again and then Esc

Using Sorted Information for System Monitoring

Sorting in htop is particularly useful for ongoing system monitoring:

  1. Press P to sort by CPU usage
  2. Leave htop running in a terminal
  3. Periodically check which processes are at the top to monitor system performance
  4. If you notice unusual resource usage patterns, you can investigate further

When you're done exploring, exit htop by pressing q.

Summary

In this tutorial, you have learned how to use the powerful htop utility to monitor and manage processes in Linux. You have gained practical skills in:

  • Installing and launching htop on Ubuntu
  • Navigating the htop interface and understanding its key components
  • Sorting processes using various criteria such as CPU usage, memory consumption, and process ID
  • Using keyboard shortcuts for efficient process sorting
  • Managing processes based on sorted information
  • Using advanced features like process filtering and tree view

These skills will allow you to effectively monitor your system's performance, quickly identify resource-intensive processes, and make informed decisions about process management. Whether you are troubleshooting performance issues, optimizing system resources, or simply keeping an eye on your system, the sorting capabilities in htop provide a valuable toolset for system administrators and Linux users of all levels.

Remember that regular monitoring of your system processes can help you maintain optimal performance and identify potential issues before they become critical problems.