Join Scott Simpson for an in-depth discussion in this video Process management: htop, part of Linux Tips Weekly.
- [Instructor] htop is a popular program for monitoring what's going on with a system's processes and resource usage. You can install from your distro's repository, and then open it up with htop. htop is a little more colorful than the regular top program. It shows some different information, and shows some information differently. One of the most striking things in the htop interface is how it represents CPU, Memory, and Swap usage as colored meters, in addition to the numeric values for the information it's showing.
The top meter is the percentage of CPU usage, showing blue for low priority processes, green for normal priority, red for kernel processes, and cyan for virtualization. The second meter shows what amount of RAM is available and used on the system, with green representing used memory, blue representing memory used as buffer, and yellow showing how much memory is being used as cache. Swap is the space on disk where things from Memory can be stored if they're in the way of other processes that need memory.
Right now, my system isn't using any, but it has four gigabytes available. Over on the right side, we can see how many tasks and threads there are. One task might have more than one thread. And you can see how many tasks are currently running, as opposed to sleeping or something else. Below that is the Load average, which represents the relative amount of work that processors are being asked to do, over the last minute, five minutes, and fifteen minutes. And on the last line here, on the right, we see how long it's been since the system was rebooted. In my case, just about two hours.
The large section below this shows the processes on the system, with columns for various information: PID for the Process ID, the user who started the process, and the priority and niceness of the process. Following that are the virtual, resident, and shared memory that the process is using. Megabytes are shown in a different color than bytes here, to make it easier to tell, at a glance, what order of magnitude is involved. And then there's the State column, where we can see most of these processes are Sleeping, or "S." "R" means Running, and in our case, here, htop is certainly running.
And there are other statuses as well, as you can see in the man pages. CPU% and MEM% show the portion of the overall processor and RAM that a process is using, and TIME+ shows the cumulative time that a process has used since it started, and after that is the command that's running. In the top program we just get a command name, but here we can see the whole command, with a path to the executable and any arguments or flags that were used to invoke it. You can scroll sideways with the arrow keys to see more of the commands, and, moving the arrows up and down, change which process is selected.
That's not too helpful right now, but it will be when we explore the functions at the bottom of the screen, accessible with the F keys. We can take a look at the Help screen with F1, or Function + F1 on some keyboard configurations. I can press F2 to go into the setup screen, where I can customize things like which meters appear. Each column has a list of what it shows, the left and the right, and I can add to these from the list on the far right. Let's add the Hostname.
I'll select that, and press Enter, and I can see it's been added to the right column. I can move it up and down with the arrow keys, Enter to confirm its location, or Delete to remove it. Over here on the left is the menu of choices, where I can change some display options, or I can choose to change the colors. Let's take a look at some of those.
I'll stick with the Default for now. I could also change which columns are active, and add more from a long list of available information. In the Active Columns list, if you select one and press Enter, you can move it around, or you can delete it with Delete or F9. I'll leave these things alone now. To get out of this screen and go back to the main one, I can press F10, but if you're using GNOME Terminal, it might grab that keypress, so instead I'll press Escape to get back to the main screen.
F3 lets me search from the Command field, and I can step through matches by pressing F3 again. I'll cancel that. F4 works the same way, but it filters the processes instead of just highlighting the matches. To clear the filter, I'll press F4 again, and then press Escape. F5 switches to a tree view, so you can see the parent and child relationship of processes, and F6 lets you choose the column to sort by.
Right now, we're sorted by CPU%, but I could change this to PID, and you can use "I" to invite the sort order. There's a cyan highlight in the header of the column that's being used for sorting. With F7 and F8, we can decrease and increase the nice value of the selected process, and with F9, we can kill a process. I'll resort my list here and select the htop process. When I press F9, I see a list of the different inerrupts that we can send a process.
SIGTERM is selected by default; that's the normal way of telling a process to end, but if something got stuck, we could change it over to SIGKILL, a more aggressive way of ending a process, the same as using "kill -9" at the command line. I'll send SIGTERM, as as expected, the process was killed. Settings you modify will persist in the .config/htoprc file, or you can create the file and add some settings in there yourself. If you work across many systems, you might consider adding that file to your process for customizing new machines.
htop is a great way of monitoring your system, and many administrators prefer it to the plain top program.
Note: Because this is an ongoing series, viewers will not receive a certificate of completion.