Monitor & Management of Linux Processes using Top Command

Posted by Asia VPN on February 10, 2024 · 4 mins read

Monitor & Management of Linux Processes using Top Command

In Linux environments, managing system processes efficiently is crucial for optimal performance and resource utilization. The top command serves as a powerful tool for real-time process monitoring, providing insights into CPU, memory, and overall system performance.

Introduction to Top Command

The top command is a versatile utility that offers a dynamic, real-time overview of system processes. When executed, it presents a continuously updated list of processes, sorted by various criteria such as CPU usage, memory consumption, and more.

Understanding the Display

Upon running the top command, the display is divided into several sections:

  • Header: Provides summary information about system uptime, total number of processes, CPU usage, memory usage, and more.
  • Process List: Displays a list of running processes, sorted by default based on CPU usage.
  • Command Bar: Offers interactive commands for managing processes, such as sorting, filtering, and killing.

Changing Display Modes

The top command allows users to customize the display according to specific monitoring requirements. Here are some common display modes:

Memory Usage Mode

To view processes sorted by memory usage:

top -o RES
    

This command sorts the process list based on the amount of memory (RES) each process is utilizing.

CPU Usage Mode

To display processes sorted by CPU usage:

top -o CPU
    

Using this command, processes are sorted based on their CPU consumption, with the most CPU-intensive processes listed at the top.

Filtering Processes by User ID

To filter processes by a specific user ID:

top -u username
    

Replace username with the desired user's username. This command displays only the processes associated with the specified user.

Killing Processes

To terminate a process using the top command:

  1. Run top.
  2. Identify the PID (Process ID) of the process you want to terminate.
  3. Press k to enter PID mode.
  4. Enter the PID of the process.
  5. Press Enter.
  6. Confirm the action by pressing y.

This process termination method provides a safe and controlled way to stop misbehaving or unnecessary processes.

Keyboard Shortcuts

While running the top command, users can utilize various keyboard shortcuts for enhanced functionality:

  • m: Toggle memory usage mode.
  • p: Toggle CPU usage mode.
  • k: Kill a process by entering its PID.
  • Shift + M: Sort processes by memory usage.
  • Shift + P: Sort processes by CPU usage.
  • Shift + U: Filter processes by user ID.
  • Shift + T: Sort processes by cumulative CPU time.
  • Shift + R: Reverse the sort order of processes.
  • Shift + H: Toggle thread display.

Examples

Let's delve into practical examples of utilizing the top command:

Example 1: View Memory Usage

top -o RES
    

This command sorts processes based on their memory utilization, allowing administrators to identify memory-intensive processes.

Example 2: View CPU Usage

top -o CPU
    

With this command, users can monitor CPU-intensive processes, helping in troubleshooting performance issues related to CPU utilization.

Example 3: Filter Processes by User ID

top -u myusername
    

By specifying a username, this command filters and displays processes associated with the specified user only.

Example 4: Kill a Process

To terminate a process with PID 1234:

top
k
1234
    

By entering the PID and confirming the action, the selected process is safely terminated, freeing up system resources.

Watch Video Tutorial

Conclusion

The top command is an invaluable tool for Linux system administrators, offering real-time insights into system processes and resource utilization. By leveraging its various display modes, keyboard shortcuts, and functionalities, administrators can effectively manage and optimize system performance.