top

top

The top command in Linux is used to display real-time system resource usage. It provides a dynamic view of processes running on the system, along with CPU, memory, and swap usage. Here’s a detailed explanation of how to use top and what information it provides:

Usage of top

Basic Usage

To use top, open a terminal and simply type:

top

By default, top displays a continually updating list of processes ordered by CPU usage.

Options and Interactive Commands

top provides a rich set of interactive commands and options to control its behavior and view different system metrics:

  1. Interactive Commands

    • Sorting: Press P to sort processes by CPU usage (default), M to sort by memory usage, N to sort by process ID, etc.

    • Navigation: Use arrow keys to scroll up and down through the process list.

    • Process Management: Press k to kill a process, r to renice a process (change its priority), u to filter processes by user, etc.

    • Toggle Display: Press f to toggle columns (fields) displayed, H to toggle threads, c to toggle full command path, etc.

    • Quit: Press q to quit top.

  2. Options

    • -d <seconds>: Specifies the delay between updates (in seconds).

    • -u <username>: Displays processes for a specific user.

    • -p <PID>: Monitor specific process IDs.

    • -b: Batch mode. Runs top in batch mode, useful for capturing output programmatically.

    Example:

    top -d 5    # Update every 5 seconds
    top -u username    # Show processes for user 'username'
    top -p 1234,5678   # Monitor processes with PID 1234 and 5678

Display Columns

The default display of top includes columns such as:

  • PID: Process ID

  • USER: User running the process

  • %CPU: Percentage of CPU time used by the process

  • %MEM: Percentage of memory used by the process

  • COMMAND: Command name or command line used to start the process

Use Cases

  • System Monitoring: top is invaluable for monitoring system performance and identifying processes that are consuming excessive CPU or memory resources.

  • Performance Analysis: Helps in troubleshooting performance issues and bottlenecks by providing real-time data on resource usage.

  • Process Management: Allows users to interactively manage processes, such as killing or renicing processes based on their resource consumption.

Conclusion

top is a powerful and versatile command-line tool for monitoring system resources and processes in real-time on Linux systems. It provides comprehensive insights into CPU, memory, and process activity, making it an essential utility for system administrators, developers, and users troubleshooting system performance. By mastering its interactive commands and options, users can effectively manage and optimize system resources.

help

top [options]

Display a dynamic real-time view of the running processes.

Options:

-d, --delay=NUMBER   Set the delay between updates in seconds.
-n, --number=NUMBER  Set the number of processes to display.
-s, --sort=COLUMN    Sort the processes by COLUMN.
-h, --help           Show this help message.

breakdown

-d, --delay=NUMBER: This option sets the delay between updates in seconds. The default is 2 seconds.
-n, --number=NUMBER: This option sets the number of processes to display. The default is 15.
-s, --sort=COLUMN: This option sorts the processes by COLUMN. The possible columns are:
CPU: CPU usage
MEM: Memory usage
PRI: Priority
NI: Nice value
VSZ: Virtual memory size
RSS: Resident set size
TTY: Controlling terminal
STAT: Process state
WCHAN: Waiting state
COMMAND: Command name
-h, --help: This option shows this help message.

Last updated