top

Basic Usage of top - Monitoring Running Processes in Real Time

The top command is one of the most important Linux system monitoring tools. It provides a real-time overview of system performance, running processes, CPU usage, memory usage, and system load.

top

Example output:

top - 20:15:01 up 5 days,  2:14,  2 users,  load average: 0.42, 0.35, 0.30
Tasks: 215 total,   1 running, 214 sleeping,   0 stopped,   0 zombie
%Cpu(s):  5.2 us,  2.1 sy,  0.0 ni, 92.1 id
MiB Mem :   7972.0 total,   2145.3 free,   3021.4 used
  • top displays live system statistics
  • Updates continuously in real time
  • Essential for troubleshooting performance issues
  • Commonly used by Linux administrators and DevOps engineers

Understanding the Header Section

The top section provides system-wide information.

Example:

top - 20:15:01 up 5 days, 2 users, load average: 0.42, 0.35, 0.30

Breakdown:

Value Meaning
20:15:01 current system time
up 5 days system uptime
2 users logged-in users
load average system workload averages

Understanding Load Average

Example:

load average: 0.42, 0.35, 0.30

These values represent:

Value Time Period
first last 1 minute
second last 5 minutes
third last 15 minutes

General interpretation:

  • Lower values usually indicate low system load
  • Higher values may indicate CPU saturation
  • On a 4-core CPU:
    • load 4.0 means full utilization
    • load 8.0 means overload

Understanding CPU Usage

Example:

%Cpu(s): 5.2 us, 2.1 sy, 92.1 id

Breakdown:

Value Meaning
us user-space CPU usage
sy kernel/system CPU usage
id idle CPU time

Example interpretation:

  • High us may indicate heavy applications
  • High sy may indicate kernel or I/O activity
  • Low id means CPU is busy

Understanding Memory Usage

Example:

MiB Mem : 7972 total, 2145 free, 3021 used

Breakdown:

Value Meaning
total total RAM
free unused RAM
used actively used RAM

Linux aggressively uses memory for caching, so low free memory is not always a problem.


Process List Overview

The lower section displays running processes.

Example:

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

Important columns:

Column Meaning
PID process ID
USER process owner
%CPU CPU usage
%MEM memory usage
COMMAND process name

Sorting by CPU Usage

Inside top, press:

P
  • Sorts processes by CPU usage
  • Most CPU-intensive processes appear first
  • Very useful during troubleshooting

Sorting by Memory Usage

Inside top, press:

M
  • Sorts by memory usage
  • Largest memory consumers appear first
  • Helpful for detecting memory leaks

Killing Processes from top

Inside top, press:

k

You will be prompted for:

PID to kill:

Then enter:

1234

Optionally specify signal:

15
  • Signal 15 = graceful termination
  • Signal 9 = force kill

Very useful during emergencies.


Changing Refresh Interval

Inside top, press:

d

Then enter refresh delay:

2
  • Updates every 2 seconds
  • Lower values increase responsiveness
  • Higher values reduce CPU overhead

Filtering Specific Users

To monitor processes for a single user:

top -u john
  • Displays only user’s processes
  • Useful on multi-user systems

Running top in Batch Mode

For scripting and logging:

top -b -n1

Breakdown:

  • -b means batch mode
  • -n1 means run once

Useful for:

  • scripts
  • monitoring systems
  • cron jobs

Saving Output to File

Example:

top -b -n1 > report.txt
  • Saves snapshot into file
  • Useful for audits and diagnostics

Displaying Specific Processes

To monitor specific PID:

top -p 1234

Monitor multiple PIDs:

top -p 1234,5678
  • Focuses monitoring on selected processes
  • Useful during troubleshooting

Showing Threads

To display threads:

top -H
  • -H enables thread view
  • Useful for multithreaded applications
  • Common for Java and database debugging

Combining Multiple Options

Example:

top -b -n1 -u root

Breakdown:

  • -b batch mode
  • -n1 single update
  • -u root filter root processes

Another example:

top -H -p 1234

This:

  • shows threads
  • monitors process 1234 only

Common Administrative Examples

Monitor system in real time:

top

Save snapshot to file:

top -b -n1 > system_report.txt

Monitor web server process:

top -p 2450

Display only root processes:

top -u root

Practical Script Example (Step-by-Step Explanation)

Script

#!/bin/bash

OUTPUT="/tmp/system_report.txt"

echo "Generating system usage report..."

top -b -n1 > $OUTPUT

echo "Report saved to $OUTPUT"

Step 1: Shebang

#!/bin/bash
  • Defines Bash interpreter
  • Ensures script executes correctly

Step 2: Defining output file

OUTPUT="/tmp/system_report.txt"
  • Stores output path in variable
  • Makes script easier to maintain

Example value:

/tmp/system_report.txt

Step 3: Displaying informational message

echo "Generating system usage report..."
  • Displays readable status message
  • Helps structure script execution

Step 4: Running top in batch mode

top -b -n1 > $OUTPUT

Breakdown:

Option Meaning
-b batch mode
-n1 run once

The > operator redirects output into file.


Step 5: Confirming completion

echo "Report saved to $OUTPUT"

Example output:

Report saved to /tmp/system_report.txt
  • Confirms successful execution
  • Displays output location

What this script does

Step-by-step flow:

  1. Defines output file
  2. Displays informational message
  3. Runs top in non-interactive mode
  4. Saves report into file
  5. Displays completion message

Why this matters in production

The top command is critical for:

  • performance troubleshooting
  • incident response
  • server monitoring
  • resource analysis
  • capacity planning

It is heavily used in:

  • Linux administration
  • DevOps workflows
  • NOC environments
  • cloud infrastructure
  • production debugging

Common Beginner Mistakes

Confusing load average with CPU percentage.

Load average:

2.00

does not mean:

2%

It represents runnable process load.

Another mistake:

Using force kill unnecessarily.

Inside top:

k

then:

9

Signal 9 immediately terminates process and may cause corruption.

Safer approach:

15

Another mistake:

Thinking low free memory automatically means problem.

Linux uses RAM aggressively for caching.


Summary

In this guide, you learned:

  • how to use top
  • understanding load average
  • monitoring CPU usage
  • monitoring memory usage
  • sorting processes
  • filtering users
  • monitoring specific PIDs
  • batch mode usage
  • thread monitoring
  • practical shell scripting with top

These skills are essential for:

  • Linux administration
  • server monitoring
  • performance troubleshooting
  • DevOps workflows
  • production support

Additional top features not covered in this guide include:

interactive color modes
cumulative CPU statistics
alternate display windows
secure mode
process renicing
custom field management
NUMA monitoring
--help: Display help information
--version: Display version information