Linux top sorting

Why We Use Top Command in Linux?

The top command in Linux is a fundamental tool for monitoring processes, providing real-time insights into system performance based on criteria like CPU and memory usage. This command generates a list of processes with detailed information such as procedure name, PID, user, CPU usage, and memory usage.

Beyond displaying a comprehensive list of processes, top also presents concise statistics at the top of the display, showcasing average system load, CPU usage, and RAM usage.

Widely recognized as a vital resource for system administrators, the top command comes pre-installed on every Linux distribution. Its interactive nature allows users to navigate through the process list and perform actions like killing processes, distinguishing it from non-interactive alternatives like ps.

Running the command with No options

$ top

An example output:

linux top

Getting the help for top command

The top command help will provide you a list of alternatives you can use to customize the output and the functions you can perform when you’re operating the top command.

In order to get the help while still the top command is running, press the ‘h’ key:

linux top help

How to quit the top command?

After executing the top command, you may simply press the ‘q’ button to quit this. If you press ‘q’ after above help command, it will quit help. Again press ‘q’ to quit top.

How to check the version of top?

To check which version of top you have, type the following command:

$ top –v
Going through the column heading of the top command

The column headings in the process list are as follows:

PID Process ID
USER The owner of this process.
PR Process priority
NI The process nice value
VIRT Number of virtual memory used by the process.
RES Number of resident memory used by the process.
SHR Number of memory used by the process.
S Status of this process.
% CPU The share of CPU time used by the process since the last upgrade
%MEM The share of physical memory used.
Display processes for a Particular User

If you want the top command to display processes being conducted by a specific user, you can use the following syntax:

$ top -u [username]

For example, the following command will display all of the processes being run by root:

$ top -u root
Sort by Memory/Cpu/Process ID/Running Time

To obtain the process consuming the most CPU or memory, only type the list.

Press the capital ‘M’ key to sort the process list by memory usage. Processes with the most memory are shown first and rest in order.

Further Reading:  What is tar Command in Linux?

Here are additional options to sort by CPU usage, Process ID, and Running Time –

  • Press ‘P‘ – sorted by CPU usage.
  • Press ‘N’ – for sorting by process id.
  • Press ‘T’ – to sort from the running time.
How to Reverse the sorting order

By default, the sorting is done in descending order. Pressing ‘R’ shall reverse the sorting order of the currently sorted column

This is the output sorted in ascending order of CPU usage. Processes consuming the least amount of CPU are shown first. An example output is shown below:

Linux top sorting

Set Interval for Refreshing the Display

By default, the top command output is refreshed every 3 seconds. To be able to change this period, hit the ‘d’ key while the top command is operating.

You may then enter the new time, in seconds, and the newest output is refreshed on the screen.

How to kill the process?

If you would like to kill a process, simply press ‘k’ when the top is running. This will bring up a prompt, which will request the process ID of the process and press enter.

Next, input the sign using which the procedure ought to be killed. If you leave this blank, the top utilizes a SIGTERM, which enables processes to terminate gracefully. If you wish to kill a process successfully, you can type in SIGKILL here. You may also type in the sign number here. By way of instance, the amount for SIGTERM is 15 and SIGKILL is 9.

How to sort by columns using top command

By default, the processes list is sorted by the %CPU column. You can change the sort column by pressing on the following:

  • P: The %CPU column.
  • M: The %MEM column.
  • N: The PID column.
  • T: The TIME+ pillar.
See Processes for a Single User

Press “u” to find the processes for one user. You’ll be prompted for the name or UID

Type the title of the UID of the person you want to track.

How to only see active tasks?

Press I to see only active tasks.

View the Absolute Path of processes

If you would like to observe the complete path of the running processes, hit the ‘c’ key while the top command is operating. In the following output, you can see that the command now shows the path of the running processes in the Control column:

Linux top abs path

Automatically Exit top Controller after a Particular Number of Refreshes

As stated earlier, you can stop the top command manually via ‘q’ or ctrl+c. If you want the top command to run until a specific number of output refreshes, you can use the following command:

$ top –n5

The top command should exit after five refreshes.

Best Practices for Using the Top Command in Linux:

  1. Basic Execution:
    • Run the top command without any options to get a quick overview of system processes.
      $ top
  2. Accessing Help:
    • Press ‘h’ while the top command is running to access a list of options and commands for customization.
      $ top
  3. Quitting the Top Command:
    • Exit the top command by pressing ‘q.’ If accessing help, press ‘q’ twice to exit.
      $ top
  4. Checking Version:
    • Determine the version of the top command in use.
      $ top -v
  5. Column Headings:
    • Understand the meanings of column headings in the process list for effective monitoring.
      • PID: Process ID
      • USER: Owner of the process
      • % CPU: CPU usage by the process
      • %MEM: Physical memory usage
  6. Filtering Processes by User:
    • Display processes for a specific user using the ‘-u’ option.
      $ top -u [username]
  7. Sorting Processes:
    • Sort processes by memory, CPU usage, process ID, or running time for better analysis.
      • Press ‘M’ for memory usage.
      • Press ‘P’ for CPU usage.
      • Press ‘N’ for sorting by process ID.
      • Press ‘T’ for sorting by running time.
  8. Reversing Sorting Order:
    • Reverse the sorting order of the current column by pressing ‘R.’
      $ top
  9. Setting Refresh Interval:
    • Change the refresh interval of the top command for real-time updates.
      • Press ‘d’ and enter the desired interval in seconds.
  10. Process Termination:
    • Kill a process interactively by pressing ‘k’ and entering the process ID and signal type (e.g., SIGTERM or SIGKILL).
  11. Changing Sort Columns:
    • Switch between sort columns to focus on different aspects of the processes.
      • Press ‘P’ for %CPU.
      • Press ‘M’ for %MEM.
      • Press ‘N’ for PID.
      • Press ‘T’ for TIME+.
  12. Viewing Processes for a Single User:
    • Press ‘u,’ and you’ll be prompted to enter the username or UID for displaying processes of a specific user.
  13. Displaying Active Tasks Only:
    • Press ‘I’ to filter and display only active tasks.
      $ top
  14. Viewing Absolute Path of Processes:
    • Press ‘c’ to show the absolute path of running processes in the COMMAND column.
      $ top
  15. Automated Exit After Refreshes:
    • Specify the number of refreshes before the top command automatically exits.
      $ top -n5

Adjust the number (5 in this example) based on your preference.

Further Reading:  What is Linux ls command?

 

FAQs – Using the Top Command in Linux:

  1. Why use the top command in Linux?
    • The top command is essential for monitoring processes, providing real-time insights into CPU and memory usage, making it a crucial tool for system administrators.
  2. How to run the top command with no options?
    • Execute the following command:
      $ top
  3. How to access help for the top command?
    • Press ‘h’ while the top command is running to view a list of options and functions.
      $ top
  4. How to quit the top command?
    • Simply press ‘q’ to exit. Pressing ‘q’ again quits the help menu if opened during runtime.
      $ top
  5. How to check the version of the top command?
    • Use the following command:
      $ top -v
  6. What are the column headings in the top command process list?
    • Key column headings include PID, USER, % CPU, %MEM, and others, providing crucial information about running processes.
      $ top
  7. How to display processes for a specific user?
    • Use the ‘-u’ option followed by the username:
      $ top -u [username]
  8. How to sort processes by memory, CPU, process ID, or running time?
    • Press ‘M’ for memory, ‘P’ for CPU, ‘N’ for process ID, and ‘T’ for running time.
      $ top
  9. How to reverse the sorting order in top command?
    • Press ‘R’ to reverse the current sorting order.
      $ top
  10. How to set the refresh interval for the top command display?
    • Press ‘d,’ enter the desired interval in seconds, and observe real-time updates.
      $ top
  11. How to kill a process using the top command?
    • Press ‘k,’ enter the process ID, and specify the signal type (e.g., SIGTERM or SIGKILL).
      $ top
  12. How to sort processes by columns using the top command?
    • Press ‘P’ for %CPU, ‘M’ for %MEM, ‘N’ for PID, and ‘T’ for TIME+.
      $ top
  13. How to view processes for a single user?
    • Press ‘u,’ and enter the username or UID when prompted.
      $ top
  14. How to see only active tasks in the top command?
    • Press ‘I’ to filter and display only active tasks.
      $ top
  15. How to view the absolute path of processes in the top command?
    • Press ‘c’ to display the absolute path of running processes in the Control column.
      $ top
  16. How to automatically exit the top command after a specific number of refreshes?
    • Use the following command, where ‘n5’ indicates the top command will exit after five refreshes.
      $ top -n5

 

Further Reading:  How to Copy Directory in Linux

Wrapping Up  Top Command

In conclusion, the top command stands out as an essential tool for monitoring processes and system resource usage in Linux. As a fundamental command, it provides a real-time overview of the top processes, offering insights into CPU and memory usage.

Throughout this tutorial, we explored various aspects of the top command, from its basic usage to advanced functionalities:

  1. Introduction to Top Command:
    • Identified as a fundamental command for process monitoring.
    • Lists processes with detailed information such as procedure name, PID, user, CPU usage, and memory usage.
  2. Interactive Features:
    • Allows users to navigate through the process list interactively.
    • Differentiates itself from non-interactive alternatives like ps.
  3. Key Commands and Options:
    • Demonstrated basic execution with no options: $ top.
    • Explored the help command (h) for a comprehensive list of options.
    • Learned how to check the version of top: $ top –v.
  4. Understanding Process Information:
    • Examined column headings, including PID, USER, PR, VIRT, RES, SHR, S, % CPU, and %MEM.
    • Explored displaying processes for a specific user and sorting by various criteria.
  5. Advanced Functions:
    • Explored sorting options by CPU usage, Memory, Process ID, and Running Time.
    • Discussed how to reverse the sorting order (R key).
    • Learned to set the refresh interval for display (d key).
  6. Process Management:
    • Explored killing processes interactively with the k key.
    • Introduced sorting columns by CPU, Memory, PID, and Time (P, M, N, T keys).
  7. Additional Features:
    • Explored viewing active tasks only (I key).
    • Revealed the absolute path of running processes (c key).
    • Demonstrated the automatic exit after a specified number of refreshes (top –n5).

Mastering the top command enhances a system administrator’s capabilities, making it a go-to tool for efficiently managing Linux processes and resources. The interactive nature and powerful features make it an indispensable asset in the Linux toolbox.