How to Use top Command in Linux + 20 Examples
As a Linux administrator, you would use monitoring tools to do your system administrative jobs perfectly. The top command in Linux enables you to have a real-time view of the processes running and system performance on your system. Running the top command displays a summary system such as CPU, memory, system load averages, a list of running processes along with their process IDs, and swap usage. The top command in Linux provides several keyboard shortcuts and extra enhancements to improve the usability of the shown data, including color, highlighting, and graphs.
Every Linux distribution comes with the top tool pre-installed. You can select how to print the results to a file, exit a process, traverse the list of processes, and much more using this interactive command. You are recommended to learn how to use the top command in Linux since it is one of the most useful tools in your toolbox. Join us with this guide to review the 20 most used Linux top command examples and start working with this strong command confidently.
Prerequisites to Use Linux top Command
To let this tutorial work correctly, provide the options below and move on.
- A Server running Linux VPS.
- A non-root user with
sudo
privileges. - Access to a terminal window.
top Command Concept and Syntax
The top command, commonly known as the table of processes, shows the tasks that the kernel is now managing in real-time, together with the processor activity of your Linux computer. Additionally, a list of the running processes’ CPU and memory usage is displayed. Real-time Monitoring, System Summary, Process-Level View, and Interactive Controls are the best features of top command. Even though it seems complicated at first, the top may be customized to show the user only the data that is pertinent to them.
Basic Syntax
To start using the top command, you can simply run it in your terminal:
top
The Linux top full command will provide you with a summary of all the activity on your machine. It updates data every three seconds by default. To quit, type the letter q to exit.
The top command syntax with all the available options and flags is like:
top -hv | -bcEeHiOSs1 -d secs -n max -u | U user -p pid(s) -o field -w [cols]
20 Examples to Use top Command in Linux
Now that the top command in Linux explained, let’s go through this guide and get skilled in top command usage in Linux. Note that you can use top command in most Linux distributions such as Ubuntu, CentOS, etc.
1. Display Running Processes
As the most common usage of the top command in Linux, simply type top on the command line to receive the details of all running Linux processes, including memory, CPU, swap, and ongoing tasks.
top
Resource usage on the dashboard and a list of Linux running processes will be displayed in the output, which is updated every three seconds by top
. You can use the Page Up and Page Down keys and the Up and Down arrows to navigate a lengthy process list. Press q to end the top.
2. Sort Running Processes
The %CPU column is used by top
by default to arrange the list of processes. To sort all Linux running processes by Process ID, press N and T keys. Also, you can press M and P keys to sort all Linux running processes by Memory and CPU usage.
For example, if you sort the list of processes by the %MEM column, you will get this:
To persist the customizations and settings and save the changes when you use top next time, press the W key. The configurations are kept in the .toprc
file in your home directory.
3. Change Display Units
To select the units that you wish top
to use for displaying the memory values, press the E key. To select the units for values to be displayed in the process list, press lowercase e.
Here are the available options you can set the dashboard memory units:
- kibibytes (KiB)
- mebibytes (MiB)
- gibibytes (GiB)
- tebibytes (TiB)
- pebibytes (PiB)
- exbibytes (EiB – applies only for memory values)
For example, if you set the dashboard memory units to gibibytes, and the process list memory units to mebibytes, you will get an output like below:
4. Send a Signal to a Running Process
The top command in Linux is also used for sending signals. You can send any signal to a process that is already running, using the top command. To input the process PID, use the k key. You can type the signal you want to send using the top command. The process is killed if a specified signal is not entered.
5. Filter Output Content
Since the top command output comes with lots of information, you might prefer to view your considered ones. So, you can change the content you need in your output. In this example, you will see how to achieve what you want by removing the sections you do not need.
- Specific statistics for each CPU core
One keystroke on your keyboard will display the specific statistics for each CPU core. Toggle core statistics by pressing 1 several times.
- CPU Usage Graph with top Command in Linux
Also, admins usually get Linux system information when they need it. Linux top command enables you to have a graphical representation of CPU usage. top can display simple ASCII graphs that show the utilization of each CPU core. Press t to turn the graphs on or off.
To change the graph display to solid block characters or remove the CPU display and task summary section, press t again.
- top Command in Linux for Memory Usage Graph
Additionally, the top command allows memory and swap memory lines to be displayed graphically. Press m to see a graphic depiction of the amount of memory used. Press m once more to erase the lines.
6. Quit top Command After X Repetition
The top command does not stop updating the statistics until the user presses q to end the program. So, you can give the top instructions to end automatically after a predetermined number of statistic refreshes.
The basic syntax is:
top -n [X]
You just need to indicate how many times you want the top to refresh the output for [X
].
7. Display Processes By Specific User
To list specific user process details, you can use the -u option. It displays all user-specific Linux running process information.
Here is the basic syntax:
top -u [user name]
In interactive mode, pressing the u key will ask for the username or user ID:
8. Display Linux Running Processes Path
top command in Linux enables you to view the process command Line. So, you just need to press the c key to view the entire command line rather than the process name. To toggle back to viewing the process name, hit c again.
9. Display Detailed Information of Process
Press the V key to view which processes were initiated or generated by other processes.
If you wish to see the hierarchy of child-parent processes on your system, you can utilize the tree view.
10. Display Running Tasks with top Command
You can use the top command in Linux to view active tasks only. Use I key to filter the output:
11. Highlight Running Process Color
top command in Linux allows you to view the active process in color, which could make it easier for you to recognize it. To add color to the display, press the z key:
The color turns red by default. To alter the color and add more customization to the display elements, use the following actions:
- Since the top options are case-sensitive, uppercase and lowercase flags come with different results. Press Z to open the color settings page:
- You can simply press some keys to change your preferred elements. Here are the relevant keys:
- S: Summary Data area.
- M: Messages and prompts.
- H: Column headings.
- T: Task information in the process list.
To confirm your choice, press Enter.
- Also, you can use one of the following keys to choose the color for the selected element:
- 0: Black.
- 1: Red.
- 2: Green.
- 3: Yellow.
- 4: Blue.
- 5: Magenta.
- 6: Cyan.
- 7: White.
To confirm your choice, press Enter.
12. Limit Process Number
Using the top command in Linux, you can limit the process number. To do this in the output, press the “n” key. Whatever the number of running processes. You are prompted to enter the desired number of processes to view at the top.
For example, to display just the top 5 processes:
13. Change Process-Renice Priority
The process priority can be changed with the top command in Linux (nice value).
To do this, take the below actions:
- Press the
r
key while running top. - Type the process ID and hit Enter when prompted.
- The software asks you to enter a new nice value once you enter the PID. After entering a new value, hit Enter.
A higher process priority is indicated by negative nice values.
14. Filter Processes
Using a filter expression, the top filter feature lets you restrict which processes are visible in the list. Pressing the “o” key will activate the filter option. You are prompted by the computer to enter a filter expression.
for example, you can use the following filter:
%MEM>5.0
Only the processes using more than 5% of the RAM are now displayed by the top command. To remove the filters, hit the = key.
15. Set How Often the top Command Refreshes
The 3.0 second screen refresh interval is the default setting. This can be altered by selecting the d option when executing the top command to provide the desired interval duration.
16. Kill Active Processes
After determining the process PID, you can kill it by using the k option without closing the top window, as demonstrated below.
17. List Sleeping Processes
top command in Linux is also used to view the list of sleeping/idle processes, press i.
18. Get top Command Help
Using the h option in the top command allows you to obtain the top command help.
19. Alternative Display Mode
Full-screen mode is the optimal way to use this example. To display four areas in the process list, press A , and then press a to move from one area to another.
20. Save Top Command Output in File
You can use the top command in Linux to save the running top command results output to file /root/.toprc
. to do this, run:
# top -n 1 -b > top-output.txt
Linux top Command Options
So far, you learned how to use top command in Linux. running top is the simplest way to use this command and display the active processes and other system information.
Let’s take a look at the top command available options and flags, which allow you to customize the view.
Option | Name | Description |
-h | Help | Shows the command help file. |
-v | Version | Shows the library version and usage prompt and then quits the program. |
-b | Batch mode | Starts top in batch mode, allowing to send the command's output to a file or other programs. top doesn't accept input in batch mode and runs until killed or until it reaches the specified iteration limit. |
-c | Toggle command line/program name | Reverses the last remembered c state. If top displayed command lines, that field now shows program names and vice versa. |
-d [secs.tenths] | Delay time interval | Prompts you to specify the delay before updating the screen, overriding the default value. The value is in seconds. The command accepts fractional seconds but doesn't accept negative numbers. |
-e [k | m | g | t | p] | Toggle task memory scaling | Allows you to change top task area memory scaling. The available scaling options are k (kibibytes), m (mebibytes), g (gibibytes), t (tebibytes), p (pebibytes). |
-E [k | m | g | t | p | e] | Toggle summary memory scaling | Allows you to change top summary area memory scaling. The available scaling options are k (kibibytes), m (mebibytes), g (gibibytes), t (tebibytes), p (pebibytes), e (exbibytes). |
-H | Threads mode operation | Specifying the -H option instructs top to display individual threads instead of a sum of all threads in each process. |
-i | Idle process toggle | Reverses the last remembered i state. When the toggle is OFF, top doesn't display the tasks that haven't used CPU resources since the last update. |
-n | Limit iteration number | Specify the maximum number of iterations you want top to produce before ending. |
-o [field name] | Overwrite sort field | Specify the field name top uses to sort tasks. Prepending a + to the field name forces top to sort high to low while prepending it with - orders tasks low to high. The -o option primarily supports automated batch mode operation. |
-O | Output field names | The -O option complements the -o option, causing top to print each of the available field names separately and then quit. |
-p [N1, N2...] | Monitor process IDs mode | Instructs top to monitor only the processes with the specified process IDs. When combined with the threads mode (-H) , top shows all the processes in each monitored PID thread group. The maximum number of PIDs is 20. Specifying the PID value 0 is considered as the top program process ID. To return to normal operation, issue any of the following commands: = , u , or U , as they are mutually exclusive. |
-s | Secure mode operation | Starts top in secure mode, even for root. |
-S | Cumulative time toggle | Reverses the last remembered S state. When the cumulative time toggle is ON, each process is listed with the CPU time it has used. |
-u | -U [ID or name] | User filter mode | Displays only the processes matching the specified user ID or user name. The -u option matches only the effective user, while the -U option matches any user - real, effective, saved, or filesystem. Prepending the user ID or user name with an exclamation point (!) causes top to display only the processes that don't match the one provided. |
-w [number] | Output width override | When used without an argument, -w instructs top to format the output using the COLUMNS= and LINES= environment variables, if applicable. Otherwise, the width is fixed at 512 columns maximum. Appending an argument to -w increases (up to 512) or decreases the output width while the row number is unlimited. The program never exceeds the actual terminal dimensions at which top was invoked. |
-1 | Single/separate CPU states toggle | Reverses the last remembered CPU state portion. Depending on the NUMA Node command toggle (2) state, either displays all CPU information in a single line or separately. |
Conclusion
In this article, you learned about the top command in Linux and reviewed 20 examples to be an expert in using this helpful command. It is a good idea to use the top command when attempting to locate an issue and address it before it impacts your system. The dashboard offers a wealth of system data and analytics to assist you in managing operations that are currently in operation, and the different settings enable you to promptly identify and address any potential problems.
If you know any other usage for top command in Linux, then the comment section is all yours.