13 Top command in Linux (Monitor Linux Server Processes)

In this article, we will learn how to monitor running processes on Linux. The Linux OS offers several commands that can be used to monitor a running process, but for checking dynamic real-time processes, we can use a command called 'TOP. This tool enables System Administrators to determine how fully real-time processes are utilized by their current system.

With every Linux distribution, the 'top' utility comes pre-installed. We can utilize the interactive command to see the summary of the current system stats, and also customize the list of processes, threads, and many other features. This guide shows you how to use the top with various options to view all the current system activities. System administrators will be able to manage system resources as well as optimize their hardware utilization by analyzing uptime, CPU usage, memory utilization, swap space usage, load average, and all the other processes that are running on their system to ascertain how much real-time processing is being used.

This guide will explain the most common 13 top commands in Linux with examples. We have tested all the examples under this section with RHEL, CentOS Stream, Fedora, and Rocky Linux.

The Global Syntax for top command in Linux

top -hv | -bcHisS -d delay -n limit -u|U user | -p pid -w [cols]

Here are the key options for TOP Command:

top command syntax

Let's first execute the top command in the terminal without any options and look at the output. The default output consists of two levels: horizontal level and vertical level. The horizontal section gathers statistics regarding processes and resource usage, while a vertical section reveals the list of all processes currently running. To terminate this session, you can press "q" on the keyboard.

top command in linux

Note:

This output shows the stats in the horizontal section indicating the uptime, load average, the number of active tasks, CPU usage, memory, and the vertical section indicating the various processes in separate columns. For a better understanding of these columns, let us look at a few.

top command output column description

1. How to identify the running process using the top command?

$ top

top command in linux

Note:

When you press the 'z' (lower case) key from the keyboard in the running top command, all the running processes will be highlighted in red color by default. This will enable us to identify the processes that are running.

top comand to running processes

2. How to display a specific user process using the top command?

$ top -u username

top command to see user processes

Note:

This situation only allows us to identify the task run by this particular user. Our example's username is "2duser". By specifying the '-u' option, you can list out all the running processes by a particular user.

3. How to show the absolute path of the process?

$ top

top command in linux

Note:

After you press the 'c' (lower case) key from the keyboard in the running top command, it displays the absolute path of the currently running program/service on the final column in the COMMAND field.

13 Top command in Linux (Monitor Linux Server Processes) 1

4. How do you sort through CPU/Memory/Process ID and Running Time utilization?

Note:

You can sort the running processes by CPU utilization by pressing the 'P' (upper case/ SHIFT + P) key on the keyboard in the running top command.

top command to sort cpu memory and processes

Note:

Similarly, you can sort out other types of resource usage, such as Memory usage, Process IDs, and Running Time, by pressing the following keys from the keyboard.
For Memory usage :  press the 'M' (upper case/ SHIFT + M)
For Process ID       : press the 'N' (upper case/ SHIFT + N)
For Running time    : press the 'T' (upper case/ SHIFT + T)
5. How to kill the running process?

Note:

To terminate a service/task, you can simply press 'k' (lower case) in the running top command. The command will display the associated process id, as shown below.

top command to kill the processes in linux

Note:

In our example, we will kill the Python3 service PID of 20357. In order to launch the process, you must enter the PID and press enter on the keyboard.

To avoid affecting your applications or their dependencies, double check before killing any process in Linux. Once you find the PID of that particular process, you can use different commands within Linux to gather more information. Additionally, you can use the PS command to find out more about that particular PID. To learn more about PS commands in Linux, you can follow this step-by-step tutorial

6. How do I set a screen refresh interval?

top -d <seconds>

top -d 1

OR

Note:

The 'd' key on the keyboard can be used to set the desired interval time when the top command is run, as shown below.

top command to set the desired interval time

Note:

The default behavior is for the top command output screen to refresh every 3 seconds. To alter the default interval, you would use the '-d' option along with a time in seconds. As you can see in the example above, we have changed the default refresh rate from 3 seconds to 1 second. Depending on your needs, the interval can be altered.

7. How to execute Top Command in batch mode?

$ top -b

Note:

The '-b' option on the top command enables batch mode. The batch mode program basically allows you to send top command output results to a file or another program. It will be primarily used, for scripting and troubleshooting.

For a better understanding of how the top command works, it is helpful to see how it sorts the output according to the Memory usage at default intervals, as shown below:

top command to enable batch mode in linux

8. How to use the top in secure mode?

$ top -s

Note:

Generally, this mode is enabled for security reasons. In the case where we have several clients with access to the system via ssh, they could use the top command to check the system's performance, but they couldn't edit its interval, restart it, or kill it. A normal user cannot change the interval time for the top, kill, and renice task once the security mode has been configured.

The screenshot below shows that by using the secure mode, normal users cannot alter the interval time of the top output screen.

top command to enable secure mode in linux

9. How to check CPU cores?

Note:

You can determine whether a server uses a multi-core CPU by pressing the numeric key '1' on the keyboard, sequentially while running the top command. A screen similar to this one will display the analytics displayed for each CPU. In our case, we have four CPU numbers.

top command to check the CPU cores

10. How to show the idle/sleeping process?

Note:

In the running top command, pressing the letter 'i' (lower case) will list the idle/sleeping processes. This will allow us to identify only the active tasks and suppress all the idle/sleeping tasks on the output screen.

top command to check the idle sleeping processes

11. How to display the Field Management?

Note:

In order to access the Field Management, you must press the f or F key on the keyboard while running the TOP command. Field Management allows us to choose which field to display on the TOP output screen. On the Field Management screen, press the SPACE bar on your keyboard to select a particular field. If it is activated, the field will be marked with the Asterix (*) symbol and the font will be marked as bold. Once you have made your selection, you can come off the screen by pressing the 'q' or ESC keys on your keyboard.

top command to access the field management console

12. How to automatically quit the Top command after a certain interval?

Note:

It is usually when you close a TOP utility program that you press 'q' on the keyboard. However, with the below command, you can set the program to automatically exit after a sequence of updates. In our example, the output screen at the top will quit after 5 cycles.

$ top -n 5

13. How to use help in Top command?

Note:

The 'h' key can be used on the keyboard while running the TOP command. Here is the complete list of top command help options.

top command to check the help options

Note:

For more information about the arguments and options, please visit the man page of Top Command.

Thank you for taking the time to read! I hope this article will help you to understand the 13 useful Top commands in Linux. Drop me your feedback/comments. If you like this article, kindly share it and it may help others as well.

Thank you!

About Divya Nerkar

I am Divya Nerkar and I have completed my Bachelor's degree in Computer Science and Engineering. The technology of Open Source excites me and I'm excited to share my knowledge with others.

View all posts by Divya Nerkar →

Leave a Reply

Your email address will not be published. Required fields are marked *

L