Htop - An Interactive Process Viewer for Linux


Introduction

System monitoring is an essential aspect of Linux administration, as it gives a clear overview of system resources usage, and the performances of various processes and applications running on a Linux system. Several tools provide this functionality, but Htop stands out due to its superior features and user-friendly interface. Htop is an interactive process viewer and system monitor that supersedes the older 'top' utility found in most Linux distributions.

Htop offers a real−time, color−coded, and graphical representation of the processes running on a system, with CPU, memory, and swap usage displayed at the top. In this article, we delve into the nuances of Htop, demonstrating its use and application through various examples.

Installation of Htop

To install Htop on Ubuntu or any other Debian-based distribution, you would use the following command −

sudo apt−get install htop

For Red Hat or CentOS −

sudo yum install htop

For Fedora −

sudo dnf install htop

Once installed, you can launch Htop by simply typing htop in the terminal.

Exploring the Htop Interface

When you first run Htop, you'll see a colorful interface with a wealth of information. The top section displays statistics about processor, memory, and swap usage. The rest of the screen is taken up by a list of currently running processes sorted by their CPU usage.

The main information on the screen includes −

  • PID − Process ID

  • USER − The user that owns the process

  • PRI − The priority of the process

  • NI − The nice value of the process

  • VIRT − Virtual memory used by the process

  • RES − Resident memory used by the process

  • SHR − Shared memory of the process

  • S − The status of the process

  • CPU% − The percentage of the CPU time the task has used

  • MEM% − The percentage of RAM the task has used

  • TIME+ − The total time the process has been running

  • Command − The command that initiated the process

Navigating and Managing Processes with Htop

One advantage of Htop over top is that you can scroll vertically and horizontally to view all processes and complete command lines. You can use the Up and Down arrows to select a process, and the Right and Left arrows to navigate the lower panel.

To kill a process in Htop, you move to the process using arrow keys, then press F9 or 'k', then hit Enter to send a SIGTERM signal to the process.

To change the priority of a process (also known as 'renicing'), select the process and press F7 to increase the nice value (making it less favorable for scheduling) or F8 to decrease the nice value (making it more favorable for scheduling).

Customizing Htop

You can customize Htop by pressing F2 or 'S'. This will bring up the setup menu, where you can configure meters, display options, and even select the colors used by Htop. Changes can be saved for future sessions by navigating to the 'Done' button and pressing Enter.

Filtering and Searching Processes

With Htop, you can filter the list of processes by pressing F4 or '' and then typing the text you want to filter. Pressing F3 or '/' will allow you to incrementally search for a process.

For instance, to filter or search for 'bash' processes, type 'bash' after pressing F4 or F3, and press Enter.

Example

 [htop 1.0.1] Tasks: 64, 174 thr; 1 running
Mem[|||||||||||             1880/7976MB]
Swp[                        0/0MB]

  PID USER     PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
 3091 root      20   0  5488  1660  1280 S  0.0  0.1  0:00.02 bash
 3095 root      20   0  5488  1664  1284 S  0.0  0.1  0:00.01 bash
 3101 root      20   0  5488  1668  1288 S  0.0  0.1  0:00.01 bash

Sorting Processes

By default, Htop sorts the processes by CPU usage. However, you can change the sort criteria by pressing the F6 or '>' key. A menu appears, and you can choose to sort by PID, USER, CPU%, MEM%, TIME+, etc.

Tree View

Htop allows you to view the processes in a tree view, which shows the parent-child relationships between processes. To switch to the tree view, press F5 or 't'.

Example

[htop 1.0.1] Tasks: 64, 174 thr; 1 running
Mem[|||||||||||             1880/7976MB]
Swp[                        0/0MB]

  PID USER     PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
 3091 root      20   0  5488  1660  1280 S  0.0  0.1  0:00.02 |-bash
 3095 root      20   0  5488  1664  1284 S  0.0  0.1  0:00.01 |-bash
 3101 root      20   0  5488  1668  1288 S  0.0  0.1  0:00.01 | `-bash

Show Only User Specific Processes

You can filter processes based on specific users. For instance, if you want to display only processes owned by the user 'root', you would press F4 or '' and then type 'root'.

Example 

[htop 1.0.1] Tasks: 64, 174 thr; 1 running
Mem[|||||||||||             1880/7976MB]
Swp[                        0/0MB]

 PID USER     PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
  1 root      20   0  10.6M  6080  3984 S  0.0  0.0  0:01.94 /sbin/init
  2 root      20   0     0     0     0 S  0.0  0.0  0:00.00 [kthreadd]3

View Processes Running on Specific CPUs

Htop allows you to view the processes running on specific CPUs. Press F2 to enter the setup menu, go to 'Display options' and select 'Detailed CPU time'. Exit the setup menu by selecting 'Done' and pressing Enter. This will show detailed CPU usage stats, broken down by user/system time, and also by individual CPUs (if your system has multiple cores).

Example 

[htop 1.0.1] Tasks: 64, 174 thr; 1 running
CPU0[|||||            11.7%]
CPU1[|||              7.4%]
CPU2[|||||            12.6%]
CPU3[||               4.6%]
Mem[|||||||||||             1880/7976MB]
Swp[                        0/0MB]

 PID USER     PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
  1 root      20   0  10.6M  6080  3984 S  0.0  0.0  0:01.94 /sbin/init
  2 root      20   0     0     0     0 S  0.0  0.0  0:00.00 [kthreadd]

Strace Process in Htop

Strace is a debugging utility that monitors the system calls and signals of a specific program. It's useful for debugging and understanding how a program interacts with the system's kernel. You can launch strace on a process directly from Htop by highlighting the process and pressing 's'.

Please note that you may need to run Htop as root to use this feature, and strace must be installed on your system.

Example 

[htop 1.0.1] Tasks: 64, 174 thr; 1 running
Mem[|||||||||||             1880/7976MB]
Swp[                        0/0MB]

 PID USER     PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
  1 root      20   0  10.6M  6080  3984 S  0.0  0.0  0:01.94 /sbin/init

Once 's' is pressed, a new terminal window will open, displaying the strace output for the selected process.

Show Processes Based on a Hierarchy

You can also view processes based on parent-child relationships. This can be very useful for understanding how processes are related. To view this, press F5 or 't'.

Example 

[htop 1.0.1] Tasks: 64, 174 thr; 1 running
Mem[|||||||||||             1880/7976MB]
Swp[                        0/0MB]

  PID USER     PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
  1 root      20   0  10.6M  6080  3984 S  0.0  0.0  0:01.94 |-/sbin/init
  2 root      20   0     0     0     0 S  0.0  0.0  0:00.00 |-[kthreadd]
  3 root       0 -20     0     0     0 S  0.0  0.0  0:00.00 | \[kworker/0:0H]

These are just a few examples of what you can do with Htop. It's a versatile tool with a plethora of options for monitoring and managing processes, making it a must-have in the toolkit of any Linux user.

Conclusion

Htop is an indispensable tool for Linux administrators, providing a detailed, real-time overview of system performance. Its colorful, user-friendly interface, combined with the ability to kill, renice processes, and sort, filter or search for specific tasks, make it a much-preferred tool over the traditional 'top' utility. With Htop, you get a comprehensive and interactive view of your system, making troubleshooting and system resource allocation a breeze.

Updated on: 17-Jul-2023

152 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements