The Top Command Output in Linux: Complete Guide

What is the Top Command in Linux?

How to Launch the Top Command

top

Understanding the Top Command Output Layout

1. Summary Information (Header Section)

  • uptime: System running time since the last boot.
  • users: Number of logged-in users.
  • load average: The average system load over the last 1, 5, and 15 minutes.
  • Tasks: Total number of processes and their states (running, sleeping, stopped, or zombie).
  • CPU Usage: Percentage of CPU usage across different states:
    • us: User processes
    • sy: System processes
    • ni: Nice value processes
    • id: Idle CPU
    • wa: I/O wait
    • hi: Hardware interrupts
    • si: Software interrupts
    • st: Stolen time by virtual machines

Example:

Tasks: 150 total, 1 running, 149 sleeping, 0 stopped, 0 zombie
%Cpu(s): 3.4 us, 0.6 sy, 0.0 ni, 95.6 id, 0.2 wa, 0.0 hi, 0.2 si, 0.0 st

2. Memory Usage

  • Mem: Total memory, used memory, free memory, and buffers/cache memory.
  • Swap: Total swap space, used swap, free swap, and cached memory.

Example:

MiB Mem :  7972.0 total,  3120.3 free,  3452.4 used,  1399.3 buff/cache
MiB Swap:  2048.0 total,  2048.0 free,     0.0 used.  3904.5 avail Mem

3. Process Table (Task List)

The process table displays detailed information about individual processes. The columns in this section are:

  • PID: Process ID
  • USER: Owner of the process
  • PR: Priority of the process
  • NI: Nice value of the process
  • VIRT: Virtual memory used by the process
  • RES: Resident memory (RAM) used by the process
  • SHR: Shared memory used by the process
  • S: Process status (Running, Sleeping, Stopped, or Zombie)
  • %CPU: CPU usage percentage
  • %MEM: Memory usage percentage
  • TIME+: Total CPU time consumed by the process
  • COMMAND: Command name that started the process

Example:

PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
1234 root      20   0   500M  120M  10M S   0.3  1.5   0:02.15 apache2
5678 user1     20   0   400M   90M   8M R   1.2  1.1   0:05.32 chrome

How to Sort Processes in Top Command

The top command allows sorting processes by different parameters using the following keys:

  • P: Sort by CPU usage
  • M: Sort by Memory usage
  • T: Sort by Time+ (CPU time)
  • N: Sort by PID

To sort by memory usage, press Shift + M while the top command is running.

Interactive Top Command Options

The top command offers several interactive options to manipulate its output:

  • k: Kill a process by entering its PID.
  • r: Renice a process by changing its priority.
  • q: Quit the top command.
  • h: Display help information.
  • d: Change update interval.

Customizing Top Command Output

You can customize the top command output to display only specific fields by pressing f and selecting the desired columns. Additionally, the top command allows switching between different display modes using the z key to toggle color highlighting.

Common Top Command Examples

  • Show top 10 processes by memory usage:
 top -o %MEM
  • Show top 10 processes by CPU usage:
 top -o %CPU
  • Refresh every 2 seconds:
 top -d 2

FAQs

1- What is the use of the top command in Linux?

2- How can I sort processes by memory usage in top command?

3- How to kill a process using the top command?

Press k, enter the PID of the process, and confirm by pressing Enter.

4- Can I customize the top command output?

Conclusion

Read Also

Resolve ORA-01172 and ORA-01151 Errors in Oracle Databases

Previous Post
Next Post