What is Top
Top is the #1 monitoring tool that any one can use in their server to get an idea of what is going on in there with server load, CPU wait, process states, memory usage etc.
The main advantage of top is that it provide you a real-time picture on the process states and it can be used as process manager to an extend where it gives you options like terminating selected processes by providing its process ID. The top command syntax is as noted below for your reference.
usage: top -hv | -abcHimMsS -d delay -n iterations [-u user | -U user] -p pid [,pid ...]
A sample screenshot of a top windows is pasted below and we can go further exploring the options based on this image.
Every Linux distribution now a days is shipped with top within its default package list. The top window can be opened by just entering top in a Linux terminal, the default format of such a windows is provided in the above image. We can proceed with a row by row explanation for the command output.
#1 Row One
Four fields of information are available in the first row output, which are current system time, system up-time, logged in users and the server load average.
#2 Row Two
This row will give you an idea on the process information from server, which are total number of processes and number of processes based on the process states such as Running processes, Sleeping processes, Stopped processes and Zombie processes.
Running : Running or runnable processes, or the processes waiting for the CPU to process it.
Sleeping : Interruptible sleep, processes waiting for an event to complete like an input or output from another process.
Stopped : Process that has been suspended/stopped
Zombie : Processes which are already dead but it has a an entry in the process table of the parent process. This can happen when the parent process die before clearing the process tables and the child process turnout to be orphan.
#3 Row Three
This row contains the information about the CPU usage in system, which provide you % CPU utilization for User processes (us), System processes (sy), Processes with Nice value (ni), Idle processes (id), Waiting for I/O (wa), Hardware Interrupts (hi), Software Interrupts (si), Steal Time (st)
#4 Row Four and Five
The fourth and fifth row describe memory information on RAM and SWAP respectively. Total assigned, used, free and buffer usage from RAM share and total, used, free and cached from assigned SWAP.
#5 Row Six
The next block of information describes more on the running processes and their individual details like the PID, ownership (USER), running priority (PR), nice value (NI), virtual (VIRT), reserver (RES), shared (SHR) memory used by the process, process status (s), CPU used by the process, RAM (MEM) used by the process, the running time of the process (TIME) and the command line expansion of the process (COMMAND).
Top Command Tips
We are going to discuss some of the flags that can be used with top command which can help you work and manipulate the output more efficiently.
#1 Sort options
You can use the key combination Shift+o or Shift+f to reach the sort option of top command. Below image shows top options for your reference.
Press the key corresponding to the criteria which you would like to get the top output sorted and press Enter.
#2 Decreasing output frequency (-d)
top is programmed to output the details in an interval of every 3 seconds, however using top in such a frequency for a system affected with high CPU load can again increase the server load. Similarly, if you are monitoring a system for a process which exist for a very short duration, the 3 seconds frequency will be high and possibly the process will not be shown in top output. The -d flag can be used with top to specify the refresh frequency of outputs such as,
top -d1 ,for one second iteration of results
top -d5 ,for five second iteration of results etc.
You can press the d key on a running top screen to adjust the delay.
#3 Color Marking Running Processes
The z key can be used in an opened top screen to get the running process for easy identification.
#4 Listing processes owned by a specific user (-u)
Trigger top command using the -u flag to get the listing only for that specific user.
Top -u <username>
You can press the u key on a running top screen to get listing for a specific user.
#5 Killing specific PIDs (k)
You can press the k key on a running top screen which will ask you to enter the process ID of specific process that you want to terminate and then it will ask you the signal that you want to use for process termination, by default it will be 15 SIGTERM, you can use 9 SIGKILL