Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Top Command in Linux with Examples

Last Updated on November 21, 2023 by Abhishek Sharma


In the world of Linux system administration, effective monitoring is crucial for maintaining optimal performance. One indispensable tool for real-time monitoring is the top command. This command provides a dynamic, real-time view of system processes, resource usage, and other vital information. In this article, we’ll explore the top command in Linux, understand its output, and delve into practical examples of how to use it.

Understanding the Basics:

The top command displays a live, continually updated list of system information. It provides a snapshot of the system’s performance, showing details such as CPU usage, memory usage, running processes, and more. To launch top, simply open a terminal and type:

top

Key Interactive Commands:

When the top is running, you can interact with it using various commands. Here are some essential interactive commands:

  • q: Quit
  • Exit the top command.
  • k: Kill
  • Prompt for the PID (Process ID) of a process to kill.
  • Space: Refresh
  • Refresh the display with updated information.
  • f: Fields
  • Access the field management and configuration screen, allowing you to choose which columns to display.
  • 1: Single CPU View
  • Toggle between a summary view of all CPUs and a detailed view for each CPU.
  • Shift + P: Sort by CPU Usage
  • Sort the process list by CPU usage.
  • Shift + M: Sort by Memory Usage
  • Sort the process list by memory usage.

Practical Examples

Here are some Examples:
Example 1: Sort Processes by CPU Usage

top -o %CPU

This command starts top and sorts the process list based on CPU usage in descending order.

Example 2: Monitor a Specific Process

top -p 

Replace with the Process ID of the specific process you want to monitor. This provides detailed information about that particular process.

Example 3: Display Specific Columns

top -b -n 1 -o %MEM | awk 'NR top_output.txt

Use the -b option to run top in batch mode for a single iteration and redirect the output to a file for future analysis.

Conclusion:
The top command in Linux is a versatile and powerful tool for real-time system monitoring. By exploring its interactive commands and practical examples, you can gain valuable insights into your system’s performance and troubleshoot issues effectively. Whether you’re a system administrator or a Linux enthusiast, mastering the top command is a key skill for maintaining a healthy and efficient Linux system.

FAQs related to Top Command in Linux:

Here are some FAQs related to top Command in Linux:

Q1: How do I launch the top command?
A: To launch the top command, open a terminal and simply type top. This will display a live, continually updated list of system information.

Q2: How can I monitor a specific process with top?
A: Use the command top -p , replacing with the Process ID of the specific process you want to monitor. This provides detailed information about that particular process.

Q3: Can I customize the columns displayed in top?
A: Yes, you can customize the columns displayed in top. Press f to access the field management and configuration screen, allowing you to choose which columns to display.

Q4: How do I sort processes based on CPU or memory usage?
A: You can sort processes based on CPU usage using Shift + P and on memory usage using Shift + M while top is running. Alternatively, you can launch top with specific sorting using options like -o %CPU or -o %MEM.

Q5: Is it possible to save top output to a file?
A: Yes, you can save top output to a file using the redirection operator. For example, top -b -n 1 > top_output.txt runs top in batch mode for a single iteration and saves the output to a file named "top_output.txt."

Leave a Reply

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