Everyone knows how useful a task manager can be. In Windows we used to use it often to kill an offending process that refuses to shut down via normal means. In Linux too, we have command line utilities like "top" that display the set of active processes.
Limitations of Inbuilt "Task Managers" in Linux
Here's a sample output from the "top" command:
Earlier, I'd written a tutorial about installing a utility called "chrony" which synchronizes the hardware and system clocks with time servers across the globe. Here we can see that this process is running in the background, along with some information like the process ID, memory used etc.
But this tool is extremely limited and is a far cry from what we're used to with the windows task manager. We can't shut down processes from this screen, or get further information like the path to the command. For all its benefits, the command line is seriously lacking when it comes to selecting one item amongst many - that's the single greatest benefit of a mouse.
Without a more sophisticated tool, we have to manage processes manually using separate commands for each. Why not have something more along the lines of a task manager in Windows?
Installing the "htop" Linux Utility Instead
We can expand the functionality of "top" using "htop" instead. It's still a command line utility, but it allows us to do a lot more with the running processes instead of just viewing them. It provides the capability to navigate through the process list and perform all kinds of tasks without quitting. Let's take a look at how to install and use it on CentOS 7.
The htop utility requires the "ncurses" API library. This allows programs to write to the command line by borrowing some "GUI like" elements, thereby allowing us to have more interactive programs like htop. So first, we need to ensure that the ncurses library is installed. On CentOS 7, this is already available by default as part of the base system build as shown here:
If you don't find it on your system, you'll need to use your package manager (like "apt-get" for Ubuntu) to install it.
Unlike the ncurses library, htop isn't available as part of the default CentOS 7 installation. First, we need to install the EPEL repositories in CentOS using the following command:
yum install epel-release
This provides the repos for a whole bunch of useful software. It's a good thing to have available in any case!
Once the EPEL repos are available, we can install htop like this:
yum install htop
Working with htop
You can start htop simply by typing:
This provides an interface similar to "top", but allows you to move the cursor and highlight individual items:
Here, you I've selected the "chrony" process. I can even use the arrow keys to scroll to the right and view the full path of the process:
Once selected, we can use the function keys to perform actions on that specific process. For example, F9 to kill it, F7/F8 to decrease/increase the priority etc. We can even restrict the display only to those processes we're interested in using the filter option F4.
Sorting by Column:
One of the most useful functions of a task manager is the ability to sort processes by column so that we can see which ones are using up the most CPU or memory. In htop, we can do this using the function key F6, which allows us to choose the column like this:'
In the above screenshot, I want to sort the processes by resident memory (RES). This shows us how much actual physical RAM is being used by any given process, as opposed to virtual memory, which can reside on disk. Sorting by RES gives us an output like this:
This is great for troubleshooting a slow server, were you can see which tasks are creating the bottleneck. And this is why htop is so useful. It takes the raw output of "top" and channels it into an interface that is much more accessible and manipulable. If you're a Linux admin, a utility like htop is invaluable for getting a quick overview of what's happening on your server.