How to Use the ‘top’ Command in Linux

How to Use the ‘top’ Command in Linux

The ‘top’ Command in Linux – Introduction

The ‘top’ command in Linux is a system monitoring tool that provides real-time information about the system’s processes and resource usage. It displays a dynamic view of the system’s overall performance and allows users to view the list of currently running processes, their resource consumption, and other system information.

When executed, ‘top’ displays a list of all the active processes on the system, with the most resource-intensive processes listed at the top. The default view of ‘top’ includes information on CPU usage, memory usage, system load, and uptime.

20 ‘top’ Command in Linux with Examples

Here are practical examples of the ‘top’ command.

  1. Displaying System Usage Summary

top

This command displays the system usage summary that includes information about the total number of processes, CPU usage, memory usage, and other system information.

  1. Specifying the Update Interval

top -d 5

The -d option specifies the update interval for the top command. This example sets the update interval to 5 seconds.

  1. Sorting by CPU Usage

top -o %CPU

The ‘-o’ option specifies the sorting order for the ‘top’ command. This example sorts the processes by CPU usage.

  1. Sorting by Memory Usage

top -o %MEM

This command sorts the processes by memory usage.

  1. Displaying Specific Processes

top -p PID1 PID2

The ‘-p’ option allows you to specify the PID(s) of the processes you want to monitor. This example monitors processes with the PIDs ‘PID1’ and ‘PID2’.

  1. Killing a Process

top -p PID -k

The ‘-k’ option allows you to kill a process directly from the ‘top’ command. This example kills the process with the PID ‘PID’.

  1. Displaying Process Tree

top -H

The ‘-H’ option displays the process tree, which shows the hierarchy of parent-child relationships between processes.

  1. Displaying Thread Information

top -H -p PID

This command displays thread information for the specified process with the PID ‘PID’.

  1. Displaying Kernel Threads

top -i

The ‘-i’ option displays kernel threads, which are processes that run in the background and are essential for the functioning of the system.

  1. Displaying Idle Processes

top -i -c

This command displays idle processes, which are processes that are waiting for input or output operations to complete.

  1. Displaying Cumulative Time

top -S

When using the ‘-S’ option, you can see the cumulative CPU time, or how much time has been spent on the CPU overall since each process began.

  1. Displaying Process Security Information

top -s

The ‘-s’ option displays security information about processes, such as the SELinux security context and the AppArmor profile.

  1. Displaying Memory Information

top -M

The ‘-M’ option displays memory information, including the total amount of memory, the amount of memory used, and the amount of memory free.

  1. Displaying Process Priority

top -n

The ‘-n’ option displays the process priority, which determines the order in which processes are scheduled to run.

  1. Displaying Disk I/O Information

top -d

The ‘-d’ option displays disk I/O information, including the amount of data read and written to the disk.

  1. Displaying Network I/O Information

top -n

The ‘-n’ option displays network I/O information, including the amount of data received and sent over the network.

  1. Displaying Process Status

top -s STATE

The ‘-s’ option allows you to filter the output of ‘top’ to show only the processes with a specific state. For example, to show only running processes, you can use ‘top -s R’. Similarly, to show only sleeping processes, you can use ‘top -s S’.

  1. Displaying User Information

top -u USER

The ‘-u’ option allows you to filter the output of ‘top’ to show only the processes owned by a specific user. For example, to show only processes owned by the ‘john’ user, you can use ‘top -u john’.

  1. Setting the Number of Processes Displayed

top -n NUM

You can instruct “top” to display a specific number of processes by using the “-n” option. For example, to display only the top 10 processes, you can use ‘top -n 10’.

  1. Saving the Output to a File

top -b -n 1 > output.txt

The ‘-b’ option runs ‘top’ in batch mode, which means it doesn’t update the screen but instead outputs the data to a file. The ‘-n’ option specifies the number of times ‘top’ should update the data before exiting, and ‘> output.txt’ redirects the output to a file called ‘output.txt.’ This example runs ‘top’ once and saves the output to a file.

 

Final Words

The ‘top’ command can be used to view Linux processes. It presents a dynamic, in-the-moment viewpoint of the system under operation. In addition to a list of the processes or threads that the Linux Kernel is now in charge of, this command frequently shows summary information about the system. As soon as you issue this command, information about processes and resource usage will be shown in the interactive command mode’s upper half. And a list of the processes that are now running can be found in the lower section.

You can learn about linux more deeply by clicking the link below

https://linuxiron.com/what-is-linux-a-whole-introduction/

Learn about the other linux commands by clicking the links below

https://linuxiron.com/echo-command-in-linux/

https://linuxiron.com/how-to-use-nice-renice-commands-in-linux/

https://linuxiron.com/how-to-use-kill-commands-in-linux/

https://linuxiron.com/a-beginners-guide-to-htop-for-process-management/

https://linuxiron.com/15-useful-yum-commands-in-linux/

Leave a Comment

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