How to Use the Linux top Command

The top command is a powerful and commonly used utility in Linux for monitoring system resource usage in real-time. It provides a dynamic, interactive view of processes running on your system, along with system-level statistics. Here’s a tutorial on how to use the top command effectively:

Step 1: Open the Terminal

To use top, open a terminal window on your Linux system. You can typically find the terminal application in your applications menu or use a keyboard shortcut like Ctrl + Alt + T.

Step 2: Launch the top Command

Simply type top in the terminal and press Enter:

top

Step 3: Understand the top Interface

When you run top, you’ll see a screen that provides a wealth of information about your system’s performance. Here’s an overview of the key sections of the top interface:

Header Information: The top portion of the screen displays system-level information, including the current time, system uptime, and the number of logged-in users.

Load Averages: You’ll see three load averages (1-minute, 5-minute, and 15-minute) that indicate the system’s workload. Lower values are generally better, and values near or above the number of CPU cores may indicate performance issues.

Tasks: This section shows the total number of processes, how many are running, sleeping, stopped, or zombie processes.

CPU Usage: The CPU section displays the percentage of CPU usage by system processes (sy), user processes (us), and idle time (id).

Memory Usage: This section shows the system’s memory utilization, including total, used, free, and cached memory.

Swap Usage: If your system uses swap space, this section provides information about swap usage.

Process List: The largest part of the top screen displays a list of running processes. By default, processes are sorted by CPU usage, with the most CPU-intensive processes at the top.

Step 4: Interact with top

While top is running, you can interact with it to perform various tasks:

Sort Processes: You can change the sorting order of processes by pressing the following keys:
P: Sort by CPU usage (default).
M: Sort by memory usage.
T: Sort by total time (cumulative CPU time).
N: Sort by process ID.
U: Prompt for a username and show only processes owned by that user.

Killing Processes: To send a signal to a process (e.g., to kill it), press k, then enter the process ID, and press Enter. Follow the prompts to confirm.

Changing Update Frequency: You can adjust the refresh rate of top by pressing the d key and entering a new update interval in seconds.

Exiting top: To exit top, simply press q.

Step 5: Additional top Options

You can customize the behavior of top by using command-line options. Here are some common options:

top -u username: Show processes for a specific user.

top -p PID: Monitor a specific process by specifying its PID.

top -c: Display full command lines for processes.

top -H: Show individual threads in the process list.

top -n 1: Run top for a specified number of iterations (in this case, once) and then exit.

top -b: Run top in batch mode, suitable for scripting.

The top command is a versatile tool for monitoring system performance and identifying resource-hungry processes. By mastering its interface and options, you can gain valuable insights into your Linux system’s behavior and take actions as needed.