The top is one of the most frequently used command in Unix like operating systems for the administrative tasks. It provides a dynamic and realtime view of the processes running in a system. You can compare it with the tasklist command or graphical task manager utility used in the Windows operating system. It also displays some other useful information such as used memory, CPU utilization, system uptime, etc.
I already explained about the information displayed in the output of the top command. This article is on the usage of top command in Linux or Unix.
Display the output of the top command
Open your terminal and execute the following command in your terminal –
top
This will display the given output in your terminal –
How to kill a process using top command
If you want to kill a process running in your system then first execute the top command and then identify the process. Note its PID and to kill it process press k and enter the PID of the process as given in the image below and then press the enter.
To kill the process press the enter once again. By default when you press k, it selects the first process displayed in the output to kill.
How to renice a process
Linux or Unix operating systems associates a priority parameter with each process which is known as nice value (NI). It can be changed by the root or a user with superuser privileges. If you think a process is more important than the other one you can change the priority of its execution. NI has a value between -20 to 19. The lower the value higher would be the priority.
If a process is already running with a nice value then you can renice it with top command. To renice a process you need root permissions, execute the top command with sudo, and then press r. This will ask you to enter the PID of the process to renice, enter it.
Now when you press enter key this will ask you to enter a value to renice the entered PID. Enter a value between -20 to 19 and then press enter. This will change the NI value of the process with the given PID.
How to display the full path of the command
To display the full path of the command press the c. Now you can see the full path of the commands in the image below –
If you want to return to the default view of the top command then press c once again.
How to list the processes by users
You can list all the processes from a certain user by pressing u. When you press u this will ask you to enter the user name whose processes are to be displayed. Provide the username and then press the enter. For example to show all the processes of user lalit –
Sort the process list
If you want to know which process is consuming the most resources then you can use the following combination of keys after executing the top command.
shift+m – to sort by memory usage
shift+p – to sort by CPU usage
shift+n – to sort by PID
shift+t – to sort by the running time
Or you can also sort the list by using option -o
with top command. For example to sort the list by CPU usage, use the following command –
top -o %CPU
Conclusion
Ok, that’s all for now. If you want to know more about the top command see its manual page by running man top
command in your terminal.