A process is an instance of a program. So, multiple instances are created by opening multiple windows. In previous articles, we have seen how we can easily use ps command to list processes. But, with ps command we only get to have static output. With top command we can see the processes in real-time. So, here we would take it further and see how to list processes with top command in Ubuntu.
List processes with top command in Ubuntu
Open a terminal and issue the following -
It would display a list of processes which are sorted as per the CPU usage. The higher the CPU usage, the higher the process lists in the order. So, by default it sorts the processes as per CPU usage. But, we can change that as well.
To exit from top, press q.
Sort processes with top command
Use the following syntax to sort any field with top command -
top -o <field_name>
For instance, to sort the data of top as per the Memory usage -
top -o %MEM
Use any of the following field_name - PID, USER, PR, NI, VIRT, RES, SHR, S, %CPU, %MEM, TIME+ and COMMAND. Note: It sorts the data in descending order. By using the above options we override the default configuration where processes were listed as per the CPU usage.
Press R to reverse sort the output.
Kill a process with top command
Sometimes a process provides no response. And, it gets stuck. We can terminate those processes easily with top. In the data, identify the Process ID (PID) associated with the process. Note, it down. Then, press k.
Thereafter, it would ask us -
PID to signal/kill
Enter the PID you noted and press Enter. Alternatively, we can provide 15 or 9 here. 15 is to terminate the process in the usual manner. 9 is to kill the process abruptly.
Specify the number of iterations
Besides, you would have noticed that if we leave the top command as it is. It keeps refreshing by itself. What if we limit that and make it quit after refreshing 'n' number of times. Use the following -
top -n <number>
For instance, if we want top to quit after 5 iterations. Then,
top -n 5
In conclusion, we have just covered the basics here. We will discuss it more in coming articles.