The top command in Linux lets you monitor currently running processes and the system resources they are using. As a system administrator, it can be the most useful tool in your toolbox, especially if you know how to utilize it. The top utility comes pre-installed with all Linux distros. Through this interactive command, you can customize how you can browse the list of processes, kill one, print the results to a file, and much more.
This article describes various ways in which you can use the top command to keep a check on your system activities as an administrator, much more efficiently.
We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system. We will be using the Ubuntu command line, the Terminal, in order to run our top command. You can open the Terminal either through the system Dash or the Ctrl+Alt+T shortcut.
The top Command Interface
In order to run the top command, open your Ubuntu command line, the Terminal, and enter the following:
You can open the Terminal either through the system Dash or the Ctrl+Alt+T shortcut.
Here is how the output of the top command looks like:
The upper part of the output shows statistics about the processes and resource usage. The lower part displays a list of currently running processes. In order to browse through the list, you can use the arrow keys or the page up and page down keys.
If you want to quit the command, you can either hit q or use the Ctrl+c shortcut that will terminate the command.
View top Command Help
The top command help will give you a list of options you can use to customize the output and the functions you can perform while you are running the top command.
In order to view help, simply hit the h key while the top command is running.
Set Interval for Refreshing the Screen
By default, the top command output is refreshed every 3 seconds. In order to change this interval, hit the d key while the top command is running. You can then enter the new time, in seconds, after which the latest output is refreshed on the screen.
Display Processes for a Specific User
If you want the top command to display processes being run by a specific user, you can use the following syntax:
$ top -u [username]
For example, the following command will display all the processes being run by root:
$ top -u root
Highlight Active Processes in Top output
When you hit the z key while your top command is running, the currently active processes will be shown in color, as follows:
View the Absolute Path of Processes
If you want to view the absolute path of the running processes, hit the c key while the top command is running. In the following output, you can see that the command now displays the path of the running processes in the Command column:
Kill a Running Process with Top Command
A very powerful feature of the top command is that you can kill running processes through it. This is especially helpful when a process becomes unresponsive and you want to get rid of it. Hit the k key while the top command is running. A prompt will ask you about the PID you want to kill. Enter the required process ID by viewing it from the list and then hit enter. The process and the corresponding application will close almost immediately.
Change Priority of a Process-Renice
Renice means changing the priority of a process. Hit the r key while the top command is running and then enter the PID of a process whose priority you want to change.
Automatically Exit top Command after a Specific Number of Refreshes
As mentioned above, you can quit the top command manually through q or ctrl+c. In case you want the top command to only run till a specific number of output refreshes, you can use the following command:
$ top -n [no-of-refreshes]
If you enter the following command in the Terminal, the top command will stay active till 3 refreshes and it will automatically quit afterward.
$ top -n 3
Save top Command Results to a Text File
You can save the current state of your system for later use if you save the output of the top command to a text file.
Here is how you can do so:
$ top -n [no-of-intervals] -b > filename.txt
For example, the following command will save the result of one output of the top command to a file named top.txt:
$ top -n 1 -b > top.txt
The file will be saved to the current user’s home folder. If you try to open the file through any of your text editors, the contents will look like this:
I have opened the file through the nano editor by entering the following command:
$ nano top.txt
Now you are well equipped with a tool that will help you in monitoring and thus managing your system resources in an optimal manner.