Understanding the top Command

Basic Usage of top

The top command in Linux displays real-time information about running processes, including CPU and memory usage, PID, user, time running, and more. It is an interactive command, meaning you can perform actions while it's running (like sorting processes).

top [OPTIONS]

Options Available with top

-b (Batch Mode)

Run in batch mode for logging or file output:

top -b

This option outputs top results in batch mode, which is useful for saving to a file.

-n (Number of Iterations)

Set the number of iterations top should output before exiting:

top -n 5

This example makes top run for 5 refreshes before it stops.

-d (Delay Time Interval)

Specify the delay between refreshes in seconds:

top -d 3

This sets the delay to 3 seconds between refreshes.

-u (User-Specific Mode)

Show processes owned by a specific user:

top -u username

This displays processes for a particular user.

-p (Monitor Specific PIDs)

Monitor specific processes by their PID:

top -p 1234

Displays only the process with the given PID, useful for tracking particular processes.

-c (Show Command Line)

Show full command line of processes instead of just the process name:

top -c

Reveals the complete command line for each process.

-H (Show Threads)

Show all threads in addition to processes:

top -H

This displays threads along with processes, useful for debugging multithreaded applications.

-i (Ignore Idle Processes)

Suppress idle or zombie processes:

top -i

Focuses only on active processes by ignoring idle or zombie processes.

-S (Cumulative Time Mode)

Display cumulative CPU time:

top -S

This option shows the total CPU time used by a task, including child processes.

-o (Sort by Field)

Sort processes by a specified field:

top -o %CPU

This example sorts processes by CPU usage.

-v (Version)

Display version information:

top -v

Shows the version of the top command installed on the system.

-h (Help)

Display help information:

top -h

Interactive Commands within top

k (Kill a Process)

To kill a process, press k and enter the PID of the process to terminate.

r (Renice a Process)

To change the priority (nice value) of a process, press r and enter the PID and new priority.

h (Help Menu)

Press h while running top to display a help menu.

q (Quit)

Press q to exit top.

Shift + P (Sort by CPU Usage)

Sort processes by CPU usage in descending order by pressing Shift + P.

Shift + M (Sort by Memory Usage)

Sort processes by memory usage in descending order by pressing Shift + M.

Shift + T (Sort by Running Time)

Sort processes by how long they have been running by pressing Shift + T.

Examples of top Command

View All Running Processes

top

This shows all running processes in real-time, including CPU, memory usage, and more.

Run top for 5 Iterations

top -n 5

This runs the top command for 5 iterations before automatically exiting.

Display Processes for a Specific User

top -u john

This displays processes owned by the user john.

Monitor a Specific Process by PID

top -p 1234

This monitors only the process with PID 1234.

Log top Output to a File

top -b -n 1 > top_output.txt

This logs the output of a single iteration of top to a file named top_output.txt.

Summary of Options

Option Description
-b Run in batch mode for logging or file output.
-n Set the number of iterations to run top.
-d Set the delay between refreshes.
-u Show processes for a specific user.
-p Monitor specific PIDs.
-c Show full command line of processes.
-H Show threads along with processes.
-i Ignore idle or zombie processes.
-S Show cumulative CPU time.
-o Sort by a specific field (e.g., CPU, memory).
-v Display version information.
-h Display help information.