Linux top Command: Real-time System Monitoring

LinuxLinuxBeginner
Practice Now

Introduction

Welcome to the lab on the Linux top command. In this lab, you will learn how to use the top command to monitor system processes and resource usage in real-time. This skill is essential for system administrators, developers, and anyone who needs to understand and manage system performance.

Imagine you are a junior system administrator responsible for maintaining a busy web server. The server has been reported to be sluggish, and you need to identify which processes are consuming the most resources. The top command will be your primary tool for this investigation, allowing you to view and analyze system activity in real-time.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/SystemInformationandMonitoringGroup -.-> linux/top("`Task Displaying`") subgraph Lab Skills linux/top -.-> lab-388500{{"`Linux top Command: Real-time System Monitoring`"}} end

Basic Usage of the top Command

Let's start by running the top command in its simplest form. This will give us a real-time, dynamic view of the system's processes.

Open a terminal and run the following command:

top

You should see a display similar to this:

top - 14:30:23 up  5:10,  1 user,  load average: 0.15, 0.22, 0.28
Tasks: 213 total,   1 running, 212 sleeping,   0 stopped,   0 zombie
%Cpu(s):  2.0 us,  1.3 system,  0.0 ni, 96.3 id,  0.3 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7824.9 total,   2576.8 free,   2935.0 used,   2313.1 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   4558.1 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 1234 user      20   0 3626108 205008  89380 S   2.0   2.6   0:45.85 gnome-shell
 5678 user      20   0  859492  51528  38060 S   1.3   0.6   0:10.91 Xorg
 9101 user      20   0  722816  36096  29088 S   0.7   0.5   0:05.62 gnome-terminal

This display is continuously updated (by default, every 3 seconds). Let's break down what we're seeing:

  1. The first line shows the current time, system uptime, number of users, and load average.
  2. The second line displays the total number of tasks and their states (running, sleeping, stopped, zombie).
  3. The third line shows CPU usage percentages.
  4. The fourth and fifth lines display memory and swap usage.
  5. The table below shows information about individual processes, sorted by CPU usage by default.

To exit top, press 'q'.

Sorting Processes in top

By default, top sorts processes by CPU usage. However, you can change this sorting in real-time. Let's explore how to sort by memory usage instead.

  1. Run the top command:
top
  1. Once top is running, press the 'M' key (uppercase). This will sort the processes by memory usage (resident set size) instead of CPU usage.

You should now see the processes reordered, with the most memory-intensive processes at the top of the list.

  1. To sort by CPU usage again, press the 'P' key (uppercase).

  2. To sort by process ID (PID), press the 'N' key (uppercase).

  3. To reverse the current sort order, press 'R' (uppercase).

Remember, you can always press 'h' or '?' while top is running to see a help screen with all available commands.

Exit top by pressing 'q' when you're done exploring.

Changing the Update Interval

By default, top updates its display every 3 seconds. However, you can change this interval. Let's set it to update every 1 second for more frequent updates.

Run top with the -d option:

top -d 1

You should now see the display updating more frequently. This can be useful when you're trying to catch short-lived processes or when you need to observe rapid changes in system activity.

To change the update interval while top is running:

  1. Press 'd' (lowercase).
  2. Enter the new delay in seconds (e.g., '0.5' for half a second).
  3. Press Enter.

Remember that very short update intervals can themselves consume significant CPU resources, so use them judiciously.

Exit top by pressing 'q' when you're done.

Displaying Specific User's Processes

As a system administrator, you might often need to monitor processes for a specific user. The top command allows you to do this easily.

Let's monitor processes for the current user (which is 'labex' in this lab environment):

top -u labex

You should now see only the processes owned by the 'labex' user.

This feature is particularly useful when you're trying to troubleshoot issues related to a specific user's activities or when you want to focus on your own processes in a multi-user system.

Exit top by pressing 'q' when you're done observing.

Displaying Only Active Processes

Sometimes, you might want to focus only on active processes and ignore idle ones. The top command provides an option for this.

Run top with the -i option:

top -i

This command will display only active processes, filtering out any process that has zero CPU usage since the last update.

This can be particularly useful when you're trying to identify which processes are currently consuming resources, without the clutter of idle processes.

Exit top by pressing 'q' when you're done observing.

Summary

Congratulations! You've completed this lab on using the top command for real-time system monitoring. Let's recap what you've learned:

  1. Basic usage of the top command to display system processes and resource usage.
  2. How to sort processes by different criteria (CPU usage, memory usage, PID) within top.
  3. Changing the update interval of top for more or less frequent updates.
  4. Monitoring processes for a specific user.
  5. Displaying only active processes, filtering out idle ones.

These skills will be invaluable for monitoring and managing system resources, particularly in scenarios where you need to identify performance bottlenecks or troubleshoot system issues.

Here are some additional top command options and interactive commands we didn't cover in this lab:

  • -b: Run in batch mode (useful for sending output to other programs or files)
  • -n: Number of iterations before top will terminate
  • 'k': Kill a process (by PID) from within top
  • 'r': Renice (change priority of) a process
  • 'c': Toggle display of command name/line
  • 'V': Display version information

Remember, effective system monitoring is crucial for maintaining system performance and stability. Regular use of the top command can help you detect and prevent resource-related issues before they significantly impact your system's performance.

Other Linux Tutorials you may like