Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux top Command: Continuously Monitor The Running Status Of The Process

In this tutorial, we will cover the top command in Linux. The top command is a system monitoring tool that provides a dynamic, real-time view of the processes running on a system. It displays information about the system's performance, such as CPU usage, memory consumption, and overall system load.

Basic Usage of the top Command

To start using the top command, simply enter top in your terminal:

top

The top command will display an updating list of processes, sorted by the percentage of CPU usage. The display consists of two main sections: the system summary, which is shown at the top of the screen, and the process list, which occupies the rest of the screen.

Understanding the System Summary

The system summary section displays information about the system's overall performance, such as:

  • load average: the average number of processes in the run queue over the last 1, 5, and 15 minutes.
  • Tasks: the total number of processes, and their states (running, sleeping, stopped, or zombie).
  • Cpu(s): the percentage of CPU usage for user processes, system processes, and idle time.
  • Mem: the total memory usage, including used, free, and cached memory.
  • Swap: the total swap usage, including used and free swap space.

Navigating the Process List

The process list section displays information about each process, such as:

  • PID: the process ID.
  • USER: the user who owns the process.
  • PR: the process priority.
  • NI: the nice value of the process (used to determine its priority).
  • VIRT: the virtual memory used by the process.
  • RES: the resident (physical) memory used by the process.
  • SHR: the shared memory used by the process.
  • S: the process state (R = running, S = sleeping, T = stopped, or Z = zombie).
  • %CPU: the percentage of CPU usage by the process.
  • %MEM: the percentage of memory usage by the process.
  • TIME+: the total CPU time used by the process since it started.
  • COMMAND: the name of the command that started the process.

Interactive Commands in top

The top command allows you to interact with the display using various keyboard commands:

  • q: Quit top.
  • h: Display help (list of available commands).
  • k: Kill a process by entering its PID.
  • r: Renice a process by entering its PID and the new nice value.
  • u: Show only processes owned by a specific user.
  • M: Sort the process list by memory usage.
  • P: Sort the process list by CPU usage (default).
  • T: Sort the process list by time (cumulative CPU time).
  • m: Toggle the display of memory information in the system summary.
  • t: Toggle the display of CPU information in the system summary.

Summary

The top command in Linux is a powerful system monitoring tool that provides a dynamic, real-time view of the processes running on a system. By understanding the information displayed in the system summary and process list sections, and using various interactive commands, you can effectively monitor and manage your system's performance.

  1. How to use the Linux top command:

    • Description: The top command in Linux is a dynamic and interactive tool that provides real-time information about system processes and resource usage.
    • Code:
      # Example: Running the top command
      top
      
  2. Real-time process monitoring with top in Linux:

    • Description: top continuously updates and displays real-time information about processes, system load, and resource usage.
    • Code:
      # Example: Real-time process monitoring with top
      top
      
  3. Interpreting the output of the top command:

    • Description: The top output includes information about CPU usage, memory usage, running processes, load averages, and more.
    • Code:
      # Example: Interpreting the top output
      top
      
  4. Sorting and filtering processes in top:

    • Description: top allows sorting processes by various criteria and filtering based on user-defined parameters.
    • Code:
      # Example: Sorting processes by CPU usage
      top -o %CPU
      
      # Example: Filtering processes by user
      top -u username
      
  5. CPU and memory usage analysis using top:

    • Description: top provides detailed information on CPU and memory usage, aiding in performance analysis and troubleshooting.
    • Code:
      # Example: Analyzing CPU and memory usage with top
      top
      
  6. Killing processes from within the top command:

    • Description: top allows sending signals to processes, including terminating or killing them.
    • Code:
      # Example: Killing a process from top
      top, select the process (k key), enter the signal (default is 15 for SIGTERM)
      
  7. Customizing the display in top on Linux:

    • Description: top provides options to customize the display, including changing the columns shown, adjusting refresh rates, and more.
    • Code:
      # Example: Customizing the top display
      top -d 5  # Set a refresh rate of 5 seconds
      
  8. Monitoring specific users or processes with top:

    • Description: top can be configured to monitor specific users or processes by applying filters.
    • Code:
      # Example: Monitoring processes of a specific user
      top -u username
      
  9. Continuous process tracking with top:

    • Description: top continuously tracks and updates the display, providing a real-time view of the system's status.
    • Code:
      # Example: Continuous process tracking with top
      top