
View active processes live with their system usage – a standard system utility top
Linux `top` is a powerful command-line utility that provides a real-time view of your system’s processes. It displays crucial information such as running processes, memory usage, and CPU utilization, making it an essential tool for system administrators and developers for performance analysis and troubleshooting.
`top` is written in C and is a standard package in most Linux distributions, included within the `procps-ng` package along with utilities like `ps`, `free`, and `kill`.
Official page of top: https://github.com/ThomasDickey/procps
Usage
To launch `top`, simply type `top` in your terminal. By default, it lists processes sorted by CPU usage in descending order.
Sorting Processes
`top` allows sorting processes based on different criteria. Use these commands while `top` is running:
P
: Sort by CPU usage (default)M
: Sort by memory usageT
: Sort by process timeN
: Sort by process ID
For example, press `M` to sort by memory usage.
Changing Refresh Interval
By default, `top` refreshes every 3 seconds. Change this by pressing `d` and entering a new interval in seconds.
Filtering Processes
Filter processes based on specific criteria:
u
: Filter by users
: Filter by state (running, sleeping, stopped, etc.)o
: Filter by command name
For example, press `u` and enter a username to filter processes by that user.
Killing Processes
`top` provides a way to kill processes directly:
k
: Kill a process by entering its process ID15
: Send the TERM signal to a process (graceful shutdown)9
: Send the KILL signal to a process (forceful shutdown)
Press `k` and enter the process ID to kill a specific process.
Similar Packages
Alternatives to `top` include:
- htop: An interactive process viewer with a user-friendly interface, tree view, color-coded display, and process search.
- glances: A cross-platform system monitoring tool with a comprehensive overview of CPU, memory, network, and disk usage.
- atop: A full-screen performance monitor with detailed information about system resources, process activity, disk activity, and network activity, with data logging capabilities.
Script Examples
Here are example scripts using `top` for automation:
Script 1: Monitor CPU Usage
#!/bin/bash
while true; do
top -b -n 1 | grep "Cpu(s)" >> cpu_usage.log
sleep 1
done
This script logs CPU usage to `cpu_usage.log` every second.
Script 2: Kill High CPU Processes
#!/bin/bash
while true; do
top -b -n 1 | awk '$9 > 50 {print $1}' | xargs kill -9
sleep 5
done
This script kills processes with CPU usage exceeding 50% every 5 seconds (use with caution!).
Script 3: Monitor Memory Usage
#!/bin/bash
while true; do
top -b -n 1 | grep "KiB Mem" >> memory_usage.log
sleep 10
done
This script logs memory usage to `memory_usage.log` every 10 seconds.
List of Top Functions and Constants
Function/Constant | Description |
---|---|
top |
Launches the top command |
P |
Sorts processes by CPU usage |
M |
Sorts processes by memory usage |
T |
Sorts processes by process time |
N |
Sorts processes by process ID |
d |
Changes the refresh interval |
u |
Filters processes by user |
s |
Filters processes by state |
o |
Filters processes by command name |
k |
Kills a process by entering its process ID |
15 |
Sends the TERM signal to a process |
9 |
Sends the KILL signal to a process |
Conclusion
Linux `top` is a fundamental command-line utility for monitoring system processes in real-time. Its insights into system performance are invaluable for identifying and resolving performance bottlenecks. It is an essential tool for system administrators, developers, and anyone needing to monitor and optimize Linux system performance.
This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.
This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification.
Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.
Key improvements and changes in this revised version:
- Clarity and Conciseness: Rephrased sentences for better readability and removed redundancies.
- Emphasis on Key Concepts: Highlighted the importance of
top
for system administrators and developers. - Code Formatting: Added
` tags around commands (like
top,
P,
M, etc.) and filenames (
cpu_usage.log,
memory_usage.log) for better visual distinction and to indicate terminal input. Used
tags within the
` tags for script examples to properly format and display the code.
- Improved Explanations: Added more context and explanations for the options and commands. Examples are clarified.
- Cautionary Notes: Added a warning about the potential dangers of
kill -9
and the high CPU process killing script. - Modern HTML Practices: While maintaining the required
class
attributes, ensured correct use and nesting of HTML elements. The updated code prioritizes semantic correctness (e.g., usinginside
- Removed Redundancy: The original introduction and conclusion were slightly repetitive. The rewritten version addresses this with more concise text.
- Accessibility: Ensures
alt
attribute is populated in the image for accessibility purposes. - Conciseness: Shortened sentences and paragraphs for better readability.
- Code Examples: The scripts have been preserved. Removed a superfluous blank line in one script.
- Overall Flow: The revised structure makes the article easier to follow and understand. The flow of topics is logical and builds upon previous concepts.
- Professional Tone: Maintained a professional and informative tone throughout the rewriting process.
- Keywords: Includes important keywords like "Linux," "top," "command-line," "system processes," "CPU usage," and "memory usage" for enhanced SEO.
- Semantic HTML: Using semantic elements like
for representing the script examples ensures better structure and meaning to the content, especially for code blocks.
- Corrected Script Syntax: the use of ">>" is meant to append to a file. I assumed that the single ">" was a typo and corrected it for script accuracy.
This revised version preserves the original content's technical information while enhancing its readability, clarity, and user-friendliness, aligning with modern web writing best practices. It also reinforces best practices while still keeping the required HTML.