大网数据广告活动
Web服务器与PHP

Use top, htop command to monitor processes

· 52CE 我爱测

This article describes how to use top and htop command line tools to monitor processes.

Applies to:   .NET in Linux

This article introduces how to use top and htop command line tools to monitor processes.

Prerequisites

As in the previous parts, this part is structured to put more emphasis on the theory and principals to follow when you start to troubleshoot. It doesn't have any prerequisites. However, you should have following items already set up if you followed all the steps of this training so far:

  • Nginx has two websites:
    • The first website listens for requests by using the myfirstwebsite host header (http://myfirstwebsite), and routes the requests to the demo ASP.NET Core application that is listening on port 5000.
    • The second website listens for requests by using the buggyamb (http://buggyamb) host header, and routes the requests to the second ASP.NET Core sample buggy application that is listening on port 5001.
  • Both ASP.NET Core applications are running as services that restart automatically when the server is rebooted, or the applications stops responding or fails.
  • A Linux local firewall is enabled and configured to allow SSH and HTTP traffic.

Goal of this part

When you troubleshoot a performance problem, you might have to monitor the CPU and memory usage of a process to understand how its resource consumption evolves over time. In Windows, you can use Task Manager to do it. Linux has several tools that aim to achieve the same thing.

This part will examine the top and htop command line tools to monitor processes.

top

To open the Task Manager equivalent in Linux, run the top command.

In this example, you can observe in the first listed process for PID 19933 that the process exhibits high CPU usage, and memory usage is also high.

Select and deselect columns

To add or remove columns in the view that the top tool provides, select <kbd>F</kbd> while you run the tool. The columns that are marked by an asterisk (*) are the ones that are selected for display. Use the arrow keys to move through the available columns, and use the Spacebar to select or deselect the columns. Then, select <kbd>Esc</kbd> to exit.

The changes will be saved for your user account. The next time that you run top, you'll see the columns that you selected.

Filter the output by users

To be able to target problematic resource consumption scenarios, it's important to know how to filter the top output. One of the most common methods is to filter processes by username. You can select <kbd>U</kbd>, and then type the username. The following screenshot shows the top tool filtering the available process snapshots for the www-data user.

When you select <kbd>Enter</kbd>, you see the processes that are run by the given user (the www-data user in this example).

When you inspect the output of the filtered top view, you can see that two of the processes are .NET processes. These two processes run the two ASP.NET Core applications, while the other two processes belong to Nginx.

You can use filters when you run the top command directly. For example, if you run the top -u www-data command, it will yield the same output as you would get if you were to open the top tool and select <kbd>U</kbd> to filter by user.

Remove idle processes

Select <kbd>I</kbd> or run the top -i command to filter the top output to show only the processes that are consuming CPU. The following screenshot shows the output of the top -i -u www-data command. Notice that the top output hides the idle processes even though there are four processes that were started by using the www-data user account. Only the process that has PID=19933 consumes CPU.

Select <kbd>I</kbd> again to toggle the switch and show the idle processes, too.

Kill processes

To kill or terminate a process, you have to send a kill signal to the process. You might recall that we used the sudo kill -9 <PID> command earlier to kill a process. You can also kill processes by using top. Select <kbd>K</kbd> to kill a process while top is running, and then type the PID of the process that you want to kill.

After you select <kbd>Enter</kbd>, top asks for the signal type. Select <kbd>Enter</kbd> one more time to send the "terminate" signal (15/sigterm).

After a few seconds, you'll see that the process for PID 122632 is missing from the list. Remember that in the lists, the "122632" process corresponds to the BuggyAmb ASP.NET Core application. Because it was configured to start automatically, you'll see that a new .NET process that has a new PID starts after the shutdown.

htop

Htop is a process viewer and a text mode application for system monitoring in real-time, similar to top. It's easy to use, and it displays a complete list of the processes that are running.

The htop tool is pre-installed in the distribution of Ubuntu Linux that is used to install the virtual server for this course. If htop isn't installed in your Linux distro, you can use the package managers in Linux to install it. (See Part 1.3 - Install .NET in Linux for details.)

To start the tool, run the htop command. You see a colorful output that resembles the following one:

Note: The bottom line indicates the function keys that you can use. Select <kbd>F6</kbd> to sort by different options, use the arrow keys to select the PERCENT_MEM column, and then select <kbd>Enter</kbd>. This sorts the processes by memory usage.

As with the top command, you can use keyboard shortcuts for several functions. For example, select <kbd>U</kbd> to select the username from a list.

However, there's something confusing in the htop output. Based on the output of the last examples, you should expect to see four processes for the www-data user. However, we see that there are many more entries. What could be causing this behavior?

The difference in the output occurs because htop shows both processes and their threads by default. Unless you want to see the threads, we recommend that you always disable thread view to make the output clearer. To disable thread view and see only the processes, select <kbd>Shift</kbd>+<kbd>H</kbd>. The following screenshot shows the processes without threads.

If you have to kill a process, select the process by using the arrow keys, select <kbd>F9</kbd>, and then select <kbd>Enter</kbd> to send the "terminate" signal.

This succinct overview should allow you to understand which processes are running and consuming resources on your system. To exit htop, select either <kbd>F10</kbd> or <kbd>Ctrl</kbd>+<kbd>C</kbd>.

Next steps

The next part of this series (Part 3.3 - Debuggers, core dumps, and collecting core dumps) discusses debuggers and dump files.

Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors (upstream author metadata: ahmetmithat). Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / IT Pro troubleshooting
适用范围
See source applicability · Source date 12/13/2024
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。