Knowing what is running on your server, and how it is performing, is obviously a very important part of server administration. Ubuntu server comes with a tool, called top, for viewing running processes, but unfortunately the format of the display is not good, and it is very difficult to make sense of what you are looking at. Luckily there is another, much better tool you can use.
htop is a very easy to use command line tool for monitoring system performance and analysing the running processes. To install this, simply run
sudo apt-get install htop
and then type ‘htop’ at the command line to run it. To exit at any time, press ‘q’.
htop is divided into a few logical sections. At the top left, there are bar graphs for CPU, Memory and Swap Memory usage. These graphs are coloured to give even more information. On the CPU graph, blue is for low-priority processes, green is normal processes and red is for kernel processes. Along with the % metric, these give you an idea of the load on your CPU and the different processes using it up.
The Memory graph shows how much of your available memory is in use. Again colours are used: green for memory in use, blue for buffers and yellow for cached memory.
Swap memory is also indicated here. Swap memory is space on your hard disk which the system writes tasks to in order to free up running memory, and if the tasks have not been used in some time. Swap memory in use is indicated by a red bar.
Over on the top right, there are metrics for the number of tasks, Load Average and Uptime.
The number of tasks is the number of processes (25 in the image above) and how many are running (1). It also shows how many threads are present (10). Threads are CPU features whereby multiple processes are run at the same time. You can remove the threads from the display by pressing Shift + H. Alternatively you can choose to also display kernel threads also by pressing Shift + K.
The load average has three figures after it. These are the average loads on your CPU over the last 1, 5 and 15 minutes. You can get an idea of how hard, and for how long, your processor has been working.
The uptime is a count of how long the server has been running for, since it was last booted up.
The rest of the screen is dominated by a detailed window showing the processes on the server. You can scroll up and down this list using the arrow keys, or Page Up and Page Down. There are a number of column headers along the top of this display. Each has a specific purpose, but some of the more important ones to recognise are:
|PID||This is the Process ID, or the unique value the server gives to each process in order to identify and keep track of it. This can be useful when you want to manually kill, or stop, a particular process|
|User||The user under which the process is running. You can view the processes for specific users by pressing ‘u’ and then selecting a user|
|NI||The ‘nice’ value of a process, which indicates its priority. Process ‘niceness’ ranges from -20 to +20, with the lowest nice values having the highest priority, and the highest nice values having the lowest priority. The default priority level is 0. If you enable kernel threads (Shift + K) you will see many high priority (-20) processes.|
|RES||‘Resident’: how much physical memory (RAM) a process is using, measured in kilobytes|
|S||The current status of a process. Running processes are marked with an ‘R’. Sleeping processes, that is processes which are not currently using CPU computing resources, are marked with an ‘S’. There other process states, including zombie ‘Z’, but these will be the most common|
|CPU%||The percentage of processor time used by a process|
|MEM%||The percentage of physical memory (RAM) used by a process|
|TIME+||How much processor time a process has used|
|Command||The name of the command that started the process. You can display/hide the path to the command by pressing ‘p’|
Along the bottom of the screen are a number of Function keys:
|F1||Open the help screen. You can also get this by pressing ‘?’|
|F2||Open the setup screen. You can change the view of the Meters along the top of the htop screen (use the spacebar to select the different display options), or even add additional meters, such as battery, hostname or a clock. You can also change the display and colour scheme of htop, and change the columns that are displayed in the main process window|
|F3||Search the htop display for a particular process name|
|F4||Filter the display for a process name. All other processes will be filtered out. Press F4 again and Escape to clear the filter|
|F5||Show a Tree view of the processes. Very often, processes will spawn ‘child’ processes as part of their operation. This Tree view shows the Parent process and the Child processes it generated|
|F6||Choose which column to sort the process display by|
|F7||Decrease the nice value, i.e. increase the processes priority. This can only be done when htop is run under sudo|
|F8||Increase the nice value, i.e. decrease its priority|
|F9||Kill, or stop, a process. You are given a choice of kill signals to use, the default should work fine|
|F10||Quit htop. Pressing ‘q’ at any time will also exit htop|
Other htop Tricks
Tagging Processes: if the display is moving a lot, or you want to make a particular process stand out, pressing the spacebar colours that process in yellow. To tag a parent process and all of its children, press ‘c’. You can use the spacebar to tag and untag as many processes as you want. To untag them all in one go, press Shift + U.
Quick Sorting: F6 will call up a menu of columns for you to sort the display by. Alternatively there are keys for quick sorting by CPU% (Shift + C), MEM% (Shift + M) and TIME+ (Shift + T). You can also choose to invert any sorted display by pressing Shift + I, if you wanted to see the processes with lowest memory usage, for example, rather than the highest
While htop offers a wealth of options, even using it in its most basic form provides a very clear and user friendly window into what is going on in your server. It is very easy to read and understand, and is an important tool when administering your own server.