BLOG POSTS
    MangoHost Blog / View active processes live with their system usage – a standard system utility top
View active processes live with their system usage – a standard system utility top

View active processes live with their system usage – a standard system utility top

Linux Top is a command-line utility that allows users to monitor the system’s processes in real-time. It provides a dynamic view of the system’s performance, displaying information about the running processes, memory usage, CPU utilization, and more. Top is a powerful tool for system administrators and developers to analyze and troubleshoot system performance issues.

Top is written in C programming language and is available as a standard package in most Linux distributions. It is a part of the procps-ng package, which also includes other useful utilities like ps, free, and kill.

Official page of top: https://github.com/ThomasDickey/procps

Usage

Once installed, top can be launched by simply typing “top” in the terminal. By default, it displays a list of processes sorted by their CPU usage in descending order.

Sorting Processes

Top provides various options to sort the processes based on different criteria. The following commands can be used to change the sorting order:

  • P: Sort by CPU usage (default)
  • M: Sort by memory usage
  • T: Sort by process time
  • N: Sort by process ID

For example, to sort the processes by memory usage, press “M” while top is running.

Changing Refresh Interval

By default, top updates the displayed information every 3 seconds. This interval can be changed by pressing the “d” key and entering a new value in seconds.

Filtering Processes

Top allows users to filter the displayed processes based on specific criteria. The following commands can be used to filter the processes:

  • u: Filter by user
  • s: Filter by state (running, sleeping, stopped, etc.)
  • o: Filter by command name

For example, to filter the processes by a specific user, press “u” and enter the username.

Killing Processes

Top provides a convenient way to kill processes directly from the interface. The following commands can be used to send signals to processes:

  • k: Kill a process by entering its process ID
  • 15: Send the TERM signal to a process
  • 9: Send the KILL signal to a process

For example, to kill a process with a specific process ID, press “k” and enter the process ID.

Similar Packages

While top is a powerful tool for monitoring system processes, there are other similar packages available that serve the same purpose. Some of the popular alternatives to top are:

  • htop: Htop is an interactive process viewer for Linux that provides a more user-friendly interface and additional features like tree view, color-coded display, and process search.
  • glances: Glances is a cross-platform system monitoring tool that provides a comprehensive overview of the system’s performance. It displays information about CPU, memory, network, disk usage, and more.
  • atop: Atop is a full-screen performance monitor that provides detailed information about system resources, process activity, disk activity, and network activity. It also allows users to log the performance data for later analysis.

Script Examples

Here are three examples of scripts that utilize the top command 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 continuously monitors the CPU usage and appends the output of the top command to a log file named “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 identifies processes with CPU usage greater than 50% and kills them using the kill command. It runs in a loop every 5 seconds.

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 monitors the memory usage and appends the output of the top command to a log file named “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 powerful command-line utility that allows users to monitor system processes in real-time. It provides valuable insights into the system’s performance and helps in identifying and troubleshooting performance issues. Top is widely used by system administrators, developers, and anyone who needs to monitor and optimize system performance. With its rich set of features and flexibility, top is an essential tool in the Linux ecosystem.



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.

Leave a reply

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