The Wayback Machine - https://web.archive.org/web/20240809093243/https://www.geeksforgeeks.org/top-command-in-linux-with-examples/
Open In App

How to Monitor System Activity in Linux | top Command

Last Updated : 12 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

top command is used to show the Linux processes. It provides a dynamic real-time view of the running system. Usually, this command shows the summary information of the system and the list of processes or threads which are currently managed by the Linux Kernel. As soon as you will run this command it will open an interactive command mode where the top half portion will contain the statistics of processes and resource usage. And Lower half contains a list of the currently running processes. Pressing q will simply exit the command mode.

Check your Version of top

The top -v command is used to display the version information of the top command itself. It shows details such as the version number and possibly other relevant information about the top command’s build or configuration. This can be useful for checking the version to ensure compatibility or to verify if specific features or bug fixes are included in the version being used.

top -v

version

How to Monitor System Activity in Linux Using `top` Command

One of the strengths of the top command is its ability to dynamically update the displayed information at regular intervals. This real-time monitoring capability enables users to observe changes in system activity, identify spikes in resource usage, and quickly respond to performance issues.

To launch the top command, open a terminal window and simply type:

top

This will display a continuously updating screen showing various system metrics. By default, processes are sorted by CPU usage in descending order, with the most resource-intensive processes listed at the top.

Monitor system activity in linux using top command

Monitor system activity in linux using top command

Here,

  • PID: Shows task’s unique process id.
  • PR: The process’s priority. The lower the number, the higher the priority.
  • VIRT: Total virtual memory used by the task.
  • USER: User name of owner of task.
  • %CPU: Represents the CPU usage.
  • TIME+: CPU Time, the same as ‘TIME’, but reflecting more granularity through hundredths of a second.
  • SHR: Represents the Shared Memory size (kb) used by a task.
  • NI: Represents a Nice Value of task. A Negative nice value implies higher priority, and positive Nice value means lower priority.
  • %MEM: Shows the Memory usage of task.
  • RES: How much physical RAM the process is using, measured in kilobytes.
  • COMMAND: The name of the command that started the process.

Uptime

When you first open the top command, the initial line, often referred to as the header or summary line, displays information similar to what you see when you use the uptime command. It shows:

  1. System Time: The current time on the system.
  2. Uptime: How long the system has been running since the last boot.
  3. Users: The number of users currently logged into the system.
  4. Load Average: This is shown in three numbers separated by commas. These numbers represent the average number of processes waiting for CPU time over the last 1 minute, 5 minutes, and 15 minutes, respectively. A value of 1.0 means the system’s CPU is fully utilized; higher values indicate potential overloading.

Tasks

  • Tasks: This section provides an overview of the total number of processes currently managed by the system.
  • total: Indicates the total count of processes currently being tracked by the system.
  • running: Represents the number of processes currently actively using CPU time.
  • sleeping: Refers to processes that are currently idle and waiting for a signal to wake up.
  • stopped: Denotes processes that have been manually stopped, typically through a signal.
  • zombie: Indicates processes that have completed execution but still have an entry in the process table.

%Cpu(s)

The %Cpu(s) line in the top command provides information about CPU usage and statistics on a Linux system. It typically includes:

  • us: Percentage of CPU time spent running user processes.
  • sy: Percentage of CPU time spent running kernel (system) processes.
  • ni: Percentage of CPU time spent running processes with a nice value (priority adjusted).
  • id: Percentage of CPU time spent idle (no work being done).
  • wa: Percentage of CPU time spent waiting for I/O operations to complete.
  • hi: Percentage of CPU time spent servicing hardware interrupts.
  • si: Percentage of CPU time spent servicing software interrupts.
  • st: Percentage of CPU time stolen from this virtual machine by the hypervisor (if virtualized).
  • MiB Memory

    The “MiB Memory” line in the top command provides information about memory usage and statistics on a Linux system. It typically includes:

    • total: Total amount of physical memory (RAM) available in MiB.
    • used: Amount of RAM currently in use by processes and the kernel.
    • free: Amount of RAM not being used at all.
    • buff/cache: Amount of memory used for buffering data and caching filesystems.
    • available: Estimate of how much memory is available for starting new applications without swapping.

    MiB Swap

    The “MiB Swap” line in the top command provides information about swap usage and statistics on a Linux system. It typically includes:

    • total: Total amount of swap space available in MiB.
    • used: Amount of swap space currently in use.
    • free: Amount of swap space that is not being used.
    • cached: Amount of swap space used for caching swap pages.

    Color and Highlighting

    In the top command, using the z key toggles color and highlighting of the process list. This feature enhances visibility by color-coding different aspects of the processes, such as CPU usage, memory consumption, and process states (running, sleeping, etc.). It helps users quickly identify resource-intensive processes or anomalies in system behavior.

    Color and Highlighting

    Only See Active Tasks

    In the top command, pressing i (lowercase “i”) toggles the display to show only active tasks. This means top will hide all idle processes (those with zero CPU usage) from the process list, focusing the display only on processes currently consuming CPU resources. This feature is particularly useful for monitoring system activity and identifying processes that are actively using CPU time.

    >

    Kill a Process with top

    In the top command, you can kill a process using the k key followed by entering the PID (Process ID) of the process you want to terminate. Here’s how you can do it:

    1. Start top by typing top in your terminal.
    2. Locate the process you want to kill using the arrow keys to navigate through the process list.
    3. Note down the PID (Process ID) of the process.
    4. Press k on your keyboard.
    5. Enter the PID of the process you want to kill and press Enter.
    6. Confirm the action if prompted.

    Kill a Process with topconfirm

    Alternative Display Mode

    In the top command, pressing the A key toggles the alternative display mode. This mode rearranges the display to show multiple windows, each focusing on different aspects of system performance, such as CPU usage, memory, and processes. It provides a more comprehensive and detailed view of system activity, making it easier to monitor various metrics simultaneously.

    Alternative Display Mode

    Examples on How to monitor system activity in Linux Using `top` Command

    1) Exit Top Command After Specific repetition:

    Top output keep refreshing until you press ‘q‘. With below command top command will automatically exit after 10 number of repetition.

    top -n 10

    2) Display Specific User Process

    top -u paras

    3) Highlight Running Process in Top: Press ‘z‘ option in running top command will display running process in color which may help you to identified running process easily

    4) Shows Absolute Path of Processes: Press ‘c‘ option in running top command, it will display absolute path of running pro

    5) Kill running process: You can kill a process after finding PID of process by pressing ‘k‘ option in running top command without exiting from top window as shown below.

    6) Sort by CPU Utilisation: Press (Shift+P) to sort processes as per CPU utilization.

    7) Shows top command syntax:

    top -h 

    top -h 8) Batch Mode: Send output from top to file or any other programs.

    top -b

    9) Secure Mode: Use top in Secure mode.

    top -s

    10) Command Line: The below command starts top with last closed state.

    Top -c

    11) Delay time: It tells delay time between screen updates.

    Top -d seconds.tenths

    Comprehensive System Monitoring with Glances on Linux

    Glances in Linux is a system monitoring tool that provides:

    1. Overview Dashboard: Displays real-time statistics on CPU, memory, disk, network, and processes.
    2. Process Table: Lists detailed information about running processes, including CPU and memory usage.
    3. Alerts and Notifications: Alerts users about critical system metrics exceeding predefined thresholds.
    4. Plugins and Extensions: Supports various plugins for additional monitoring capabilities, such as Docker and SNMP.
    5. Interactive UI: Offers an interactive command-line interface or a web-based interface for monitoring multiple systems remotely.

    Are There Alternatives to Top?

    • htop: An interactive and visually appealing process viewer with color-coding, easy navigation, and process management features.
    • Glances: A cross-platform monitoring tool that provides a comprehensive view of system performance, including CPU, memory, disk I/O, and network usage.
    • nmon: A performance monitoring tool designed for AIX and Linux systems, offering detailed statistics and a user-friendly interface.
    • atop: An advanced system and process monitor that provides detailed resource consumption for both current and historical data, including CPU, memory, disk, and network usage.
    • dstat: A versatile resource statistics tool that combines the functionality of vmstat, iostat, netstat, and ifstat into a single command, offering real-time performance monitoring.
    • sar: Part of the sysstat package, it collects and reports on various system activity statistics, including CPU, memory, and I/O usage, with the ability to save data for historical analysis.

    Conclusion

    In this article we discussed the top command which is an essential tool for Linux users and system administrators, offering real-time insights into system processes and resource usage. Its dynamic interface, interactive commands, and customization options make it a powerful tool for monitoring and troubleshooting. From sorting processes and killing tasks to specific user monitoring, the top command provides a concise yet comprehensive view of system health, enabling users to maintain optimal performance efficiently.

    How to Monitor System Activity in Linux | top Command – FAQs

    What is the top command in Linux, and how does it help monitor system activity?

    The `top` command is a built-in utility in Linux that provides a real-time, dynamic view of the system’s performance. It displays information about CPU usage, memory usage, running processes, and more, allowing users to monitor and analyze system activity effectively.

    How do I interpret the information displayed by the top command?

    The information displayed by the `top` command includes columns such as PID, %CPU, %MEM, VIRT, RES, and COMMAND. PID is the process ID, %CPU represents CPU usage, %MEM shows memory usage, VIRT is virtual memory, RES is physical RAM usage, and COMMAND displays the name of the process.

    Can I customize the display of the top command to focus on specific processes or users?

    Yes, the `top` command is highly customizable. Users can press keys to toggle between different views, sort processes based on various criteria, and filter the display to show processes for a specific user. For example, pressing ‘u’ allows filtering by username.

    How can I use the top command to identify and terminate resource-hungry processes?

    The `top` command allows users to interactively manage processes. Pressing ‘k’ enables the user to kill a specific process by entering its PID. Sorting processes by CPU or memory usage (with ‘P’ or ‘M’) helps identify resource-intensive tasks that may need attention.

    Are there additional advanced features of the top command beyond real-time monitoring?

    Yes, the `top` command offers advanced features. Users can run `top` with specific options, such as ‘-n’ to exit after a specific number of repetitions, ‘-u’ to display processes for a specific user, ‘-b’ for batch mode (useful for scripting), and ‘-d’ to set the delay time between screen updates. Understanding these options enhances the utility of the `top` command in diverse scenarios.



    Previous Article
    Next Article

    Similar Reads

    Sysmon – Graphical System Activity Monitor for Linux
    Sysmon is a graphical system monitor for Linux. It shows the information about the CPU, GPU, Memory, HDD/SDD and network connections. It is similar to the Windows task manager. It is completely written into the python programming language. Sysmon shows the all information in the form of Graphical visualization. There are some features of these tool
    2 min read
    Logcheck Tool - Monitor Kali Linux System Log Activity
    Logcheck is a package or tool to check system log files for security violations and unusual activity, it utilizes the program called logtail remembering the last position is read from the log file. Analyzes security or unusual activity from syslog to monitor Apache log files for errors caused by PHP Scripts. Logcheck is used to detect problems auto
    4 min read
    SAR command in Linux to monitor system performance
    sar (System Activity Report) It can be used to monitor Linux system's resources like CPU usage, Memory utilization, I/O devices consumption, Network monitoring, Disk usage, process and thread allocation, battery performance, Plug and play devices, Processor performance, file system and more. Linux system Monitoring and analyzing aids in understandi
    6 min read
    Progress - Tool to monitor progress of basic command in Linux
    Progress is a command-line-based tiny tool which is formerly known as a Coreutils Progress Viewer. This tool is written in the C language. This command looks for the Coreutils basic command like cp, mv, dd, tar, gzip, cat, etc. which are currently running on the system and display the percentage of copied data. This tool can also show the estimated
    2 min read
    How to Monitor Linux Commands Executed by System Users in Real-time?
    If you are a Linux system administrator then you would definitely want to monitor the interactive activity of all system users, like monitoring the Linux commands they execute in real-time. In this brief Linux system security article, we will be going to discuss how to monitor all Linux shell commands executed by system users in real-time. Let's se
    1 min read
    Installing atop Tool To Monitor the System Process in Linux
    atop is an ASCII full-screen interactive performance monitor which is kind of similar to the top command to view the load over a Linux system. The job of the most critical hardware resources (from a performance point of view) at the system level, i.e. CPU, memory, disk, and network can be seen. atop enable us to identify which processes are actuall
    7 min read
    How to Monitor System Usage, Outages and Troubleshoot Linux Servers
    As we know that Linux is very reliable and a great system administrator should always know the system's behavior and its utilization at every moment of time. So if an administrator or a user wants to monitor its complete system then they have to do the following Storage Space Utilization Linux provides two commands which are used to inspect storage
    5 min read
    Using htop to Monitor System Processes on Linux
    htop a Linux tool that is used in process-managing and terminal-based system monitoring. It allows real-time monitoring of processes and performs every task to monitor the process in the Linux system. The tool is written in the C programming language by Hisham Muhammad. It displays a complete list of processes running on the system and gives inform
    5 min read
    saidar Utility - Monitor the Linux Box
    Saidar is a curses-based (Curses-based software is software whose user interface is implemented through the curses library) tool for viewing system statistics and monitors the Linux system. It is a command-line based tool as it is run on a terminal. To install saidar on Debian based Linux system run the following command on the terminal: sudo apt-g
    3 min read
    Monitor and Find Statistics for Linux Processes Using pidstat Tool
    Pidstat is a command-line tool and part of sysstat suite to monitor the Linux system. It is used to monitor every individual task currently being managed by the Linux kernel on the Linux system. Features of pistat: It can monitor every task running on the systemIt can also monitor the child's task of any task.It shows the total CPU usage by every t
    4 min read
    three90RightbarBannerImg