Ubuntu Server 18.04 – Utilizing htop

Getting Started with the IIS Manager in IIS

When wanting to view the overall performance of your server, nothing beats htop. Although not typically installed by default, htop is one of those utilities that I recommend everyone installs, since it’s indispensable when wanting to check on the resource utilization of your server. If you don’t already have htop installed, all you need to do is install it with apt:

sudo apt install htop 

When you run htop at your shell prompt, you will see the htop application in all its glory. In some cases, it may be beneficial to run htop as root, since doing so does give you additional options such as being able to kill processes, though this is not required:

Running htop

At the top of the htop display, you’ll see a progress meter for each of your cores (the server in my screenshot only has one core), as well as a meter for memory and swap. In addition, the upper portion will also show you your Uptime, Load average, and the number of Tasks you have running. The lower section of htop‘s display will show you a list of processes running on your server, with fields showing you useful information such as how much memory or CPU is being consumed by each process, as well as the command being run, the user running it, and its PID. To scroll through the list of processes, you can press Page Up or Page Down or use your arrow keys. In addition, htop features mouse support, so you are also able to click on columns at the top in order to sort the list of processes by that criteria. For example, if you click on MEM% or CPU%, the process list will be sorted by memory or CPU usage respectively. The contents of the display will be updated every two seconds.

The htop utility is also customizable. If you prefer a different color scheme, for example, you can press F2 to enter Setup mode, navigate to Colors on the left, and then you can switch your color scheme to one of the six that are provided. Other options include the ability to add additional meters, add or remove columns, and more. One tweak I find especially helpful on multicore servers is the ability to add an average CPU bar. Normally, htop shows you a meter for each core on your server, but if you have more than one, you may be interested in the average as well. To do so, enter Setup mode again ( F2), then with Meters highlighted, arrow to the right to highlight CPU average and then press F5 to add it to the left column. There are other meters you can add as well, such as Load average, Battery, and more.

Depending on your environment, function keys may not work correctly in Terminal programs such as htop, because those keys may be mapped to something else. For example, F10 to quit htop may not work if F10 is mapped to a function within your Terminal emulator, and using a virtual machine solution such as VirtualBox may also prevent some of these keys from working normally.

When you open htop, you will see a list of processes for every user on the system. When you have a situation where you don’t already know which user/process is causing extreme load, this is ideal. However, a very useful trick (if you want to watch a specific user) is to press U on your keyboard, which will open up the Show processes of: menu. In this menu, you can highlight a specific user by highlighting it with the up or down arrow keys and then pressing Enter to only show processes for that user. This will greatly narrow down the list of processes.

Another useful view is the Tree view, which allows you to see a list of processes organized by their parent/child relationship, rather than just a flat list. In practice, it’s common for a process to be spawned by another process. In fact, all processes in Linux are spawned from at least one other process, and this view shows that relationship directly. In a situation where you are stopping a process only to have it immediately re-spawn, you would need to know what the parent of that process is in order to stop it from resurrecting itself. Pressing F5 will switch htop to Tree view mode, and pressing it again will disable the Tree view.

As I’ve mentioned, htop updates its stats every two seconds by default. Personally, I find this to be ideal, but if you want to change how fast it refreshes, you can call htop with the -d option and then apply a different number of seconds (entered in tenths of seconds) for it to refresh. For example, to run htop but have it update every seven seconds, start htop with the following command:

htop -d 70 

To kill a process with htop, use your up and down arrow keys to highlight the process you wish to kill and press F9. A new menu will appear, giving you a list of signals you are able to send to the process with htop. SIGTERM, as we discussed before, will attempt to gracefully terminate the process. SIGKILL will terminate it uncleanly. Once you highlight the signal you wish to send, you can send it by pressing Enter or cancel the process with Esc.

As you can see, htop can be incredibly useful and has (for the most part) replaced the legacy top command that was popular in the past. The top command is available by default in Ubuntu Server and is worth a look, if only as a comparison to htop. Like htop, the top command gives you a list of processes running on your server, as well as their resource usage. There are no pretty meters and there is less customization possible, but the top command serves the same purpose. In most cases, though, htop is probably your best bet going forward.

Comments are closed.