Skip to content

top: Process Monitoring in Linux

Summary

The top command provides a dynamic real-time view of running processes in the system. It displays CPU usage, memory usage, and other important metrics, allowing you to identify resource-intensive processes and troubleshoot performance issues.

Introduction

The top command is a powerful tool for monitoring system resources and process activity. It presents a constantly updated view of the most resource-intensive processes running on your system. This makes it invaluable for system administrators and developers who need to understand how their system is performing. Unlike commands that provide a snapshot, top updates its output periodically (by default, every three seconds), giving you a near real-time picture. It provides information such as CPU usage, memory usage, process ID (PID), user owning the process, and the command being executed. This makes it an essential utility for identifying performance bottlenecks and troubleshooting resource issues.

Use case and Examples

Basic Usage

top
This command starts top in interactive mode, displaying the process list and system summary. The display will update every three seconds by default. You can then use interactive commands within the top interface to sort, filter, and manage processes.

Specifying Update Interval

top -d 1
This command runs top with an update interval of 1 second. The -d option allows you to customize the delay between screen updates. Lower values provide a more real-time view, but can increase system load.

Displaying Processes for a Specific User

top -u username
This command runs top and filters the process list to only show processes owned by the specified username. Replace username with the actual user you want to monitor.

Sorting by CPU Usage

Within the top interactive interface, press P (Shift+p).

# Inside top
P
After pressing 'P', the processes will be sorted based on CPU utilization, with the most CPU-intensive processes appearing at the top of the list.

Sorting by Memory Usage

Within the top interactive interface, press M (Shift+m).

# Inside top
M
After pressing 'M', the processes will be sorted based on memory usage, with the processes consuming the most memory appearing at the top.

Killing a Process

Within the top interactive interface, press k.

# Inside top
k
top will prompt you for the PID of the process to kill. Enter the PID and press Enter. You will then be prompted for the signal to send (usually 15 for a graceful shutdown, or 9 for a forced kill).

Commonly used flags

Flag Description Example
-d delay Specifies the delay between screen updates in seconds. top -d 0.5 (updates every half second)
-u username Displays processes owned by the specified user. top -u www-data (shows processes owned by the 'www-data' user)
-p pid Monitors only the specified process ID(s). top -p 1234 (monitors process with PID 1234)
-n iterations Specifies the number of iterations before top exits. top -n 5 (runs top for 5 iterations and then quits)
-b Batch mode operation. Useful for sending output to a file for analysis. top -b -n 1 > top_output.txt (captures a single iteration of top output to a file)
-H Show threads. By default top shows processes; this flag shows individual threads. top -H (shows all threads running on the system)


Share on Share on

Comments