Linux top command


The top command in Linux is a real-time system monitoring tool that provides a dynamic view of system processes and resource usage, including CPU, memory, and load information. It allows administrators to identify processes consuming excessive resources and to manage system performance by prioritizing, killing, or renicing processes.

1. Basic Syntax

To run top, simply enter:

top

The output is updated in real time, typically every few seconds, and provides a snapshot of the current system state.

2. Understanding the top Command Output

The top output is divided into several sections:

A. System Information Summary (Header Section)

The first few lines in top provide a system overview, including uptime, load averages, tasks, CPU usage, and memory usage.

  1. Uptime and Load Average

    top - 15:43:02 up 1 day, 2:10, 1 user, load average: 0.50, 0.75, 0.80
    • 15:43:02: Current time.
    • up 1 day, 2:10: System uptime (1 day, 2 hours, 10 minutes).
    • 1 user: Number of users currently logged in.
    • load average: CPU load averages over the last 1, 5, and 15 minutes.

    Load average values provide a sense of CPU demand:

    • Values close to or above the number of CPU cores indicate a heavily loaded CPU.
  2. Tasks

    Tasks: 100 total, 2 running, 98 sleeping, 0 stopped, 0 zombie
    • total: Total number of processes.
    • running: Number of actively running processes.
    • sleeping: Processes in a waiting state.
    • stopped: Processes stopped or suspended.
    • zombie: Zombie (defunct) processes that are waiting to be cleaned up.
  3. CPU Usage

    %Cpu(s): 5.3 us, 2.0 sy, 0.0 ni, 90.0 id, 0.7 wa, 0.0 hi, 2.0 si, 0.0 st
    • us (user): CPU time spent on user-level processes.
    • sy (system): CPU time on kernel-level processes.
    • ni (nice): CPU time for processes with adjusted nice values.
    • id (idle): Percentage of time the CPU is idle.
    • wa (iowait): CPU time waiting for I/O operations.
    • hi (hardware interrupts) and si (software interrupts): CPU time handling interrupts.
    • st (steal): Time stolen by the hypervisor in virtualized environments.
  4. Memory Usage

    MiB Mem : 7911.7 total, 5100.3 free, 2000.1 used, 811.3 buff/cache MiB Swap: 2048.0 total, 2048.0 free, 0.0 used, 1000.0 avail Mem
    • total: Total physical memory.
    • free: Available (unused) memory.
    • used: Memory currently in use.
    • buff/cache: Memory used for disk caching and buffers.
    • Swap: Shows total, free, and used swap space, along with available memory for applications.

B. Process List (Main Section)

The lower part of top lists individual processes with columns for process ID, user, priority, and resource usage.

Key columns include:

ColumnDescription
PIDProcess ID.
USERUser who owns the process.
PRPriority of the process. Lower values indicate higher priority.
NINice value, affecting process priority.
VIRTVirtual memory size of the process.
RESResident memory used (actual physical memory).
SHRShared memory used by the process.
%CPUCPU usage as a percentage.
%MEMMemory usage as a percentage of total RAM.
TIME+Total CPU time consumed by the process.
COMMANDCommand that started the process.

Example:

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3245 john 20 0 512000 92000 13000 S 3.5 1.2 1:23.45 firefox 2134 root 20 0 248000 45000 8000 R 7.0 0.6 0:25.67 apache2 4021 jane 20 0 85000 25000 3000 S 1.2 0.3 0:03.12 vim

In this example:

  • Process firefox (PID 3245) is using 3.5% of the CPU and 1.2% of the memory.
  • apache2 (PID 2134) is using 7% of the CPU and 0.6% of the memory.

C. Interacting with top

While top is running, you can use keyboard shortcuts to interact with it:

  • q: Quit top.
  • k: Kill a process by specifying its PID.
  • r: Renice a process (change its priority).
  • M: Sort processes by memory usage.
  • P: Sort processes by CPU usage (default).
  • T: Sort processes by cumulative time.
  • 1: Show CPU usage per core (useful on multi-core systems).
  • h: Display help and list all key commands.

3. Customizing top Output

  • Changing Update Interval: Press d and enter a new update interval in seconds.
  • Filtering by User: Press u and type a username to display only that user’s processes.
  • Highlighting Active Processes: Press z to toggle highlighting of active tasks.

4. Example Usage of top

  1. Monitoring CPU Usage: If a process is consuming too much CPU, you can identify it by sorting with %CPU.
  2. Killing a Process: Use k, enter the PID of the process, and confirm to kill a misbehaving process.
  3. Changing Priority (Renice): Use r to renice a process by adjusting its NI value, lowering its CPU priority.

5. Exiting top

To exit top, simply press q.

Summary

The top command is a powerful tool for monitoring Linux system performance, especially useful for identifying and managing resource-intensive processes.