Linux Task Displaying

LinuxLinuxBeginner
Practice Now

Introduction

Monitoring system processes is a critical skill for Linux users and system administrators. Understanding what processes are running and how they consume resources allows you to maintain system stability and performance.

In this lab, you will learn how to use the top command, a powerful tool for real-time monitoring of Linux system processes. The top command provides detailed information about CPU usage, memory consumption, and other important system metrics. By mastering this essential command, you will be able to identify resource-intensive processes and effectively manage your Linux system resources.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/FileandDirectoryManagementGroup(["File and Directory Management"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux(("Linux")) -.-> linux/VersionControlandTextEditorsGroup(["Version Control and Text Editors"]) linux/BasicSystemCommandsGroup -.-> linux/echo("Text Display") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/FileandDirectoryManagementGroup -.-> linux/cd("Directory Changing") linux/SystemInformationandMonitoringGroup -.-> linux/top("Task Displaying") linux/VersionControlandTextEditorsGroup -.-> linux/nano("Simple Text Editing") subgraph Lab Skills linux/echo -.-> lab-271407{{"Linux Task Displaying"}} linux/cat -.-> lab-271407{{"Linux Task Displaying"}} linux/cd -.-> lab-271407{{"Linux Task Displaying"}} linux/top -.-> lab-271407{{"Linux Task Displaying"}} linux/nano -.-> lab-271407{{"Linux Task Displaying"}} end

Introduction to the top Command

The top command is a fundamental Linux utility that displays a dynamic, real-time view of the processes running on your system. The name "top" refers to its default behavior of showing the "top" CPU-consuming processes.

Let's start by navigating to your project directory and exploring the basic usage of the top command:

cd ~/project

Now, run the top command:

top

When you execute this command, you should see an interactive display similar to the following:

top - 14:25:30 up 2 days,  3:45,  1 user,  load average: 0.15, 0.20, 0.25
Tasks: 105 total,   1 running, 104 sleeping,   0 stopped,   0 zombie
%Cpu(s):  2.0 us,  1.0 sy,  0.0 ni, 97.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   4096.0 total,   2841.3 free,    845.2 used,    409.5 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   2970.5 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 1234 labex     20   0  562340  42340  28456 S   2.0   1.0   0:30.25 firefox
 2345 labex     20   0   78912  23456  13204 S   0.7   0.6   0:12.34 terminal
 3456 root      20   0   43528   5424   4356 S   0.3   0.1   0:05.67 sshd
  ... (more processes)

The top display consists of two main sections:

  1. Summary Area (top 5 lines): Provides system-wide information including:

    • System uptime and load averages
    • Task statistics (total, running, sleeping, etc.)
    • CPU usage percentages
    • Memory usage (RAM and Swap)
  2. Process List (table below summary): Lists running processes with details such as:

    • PID: Process ID
    • USER: The owner of the process
    • PR: Priority
    • NI: Nice value
    • VIRT: Virtual memory used
    • RES: Resident memory used
    • %CPU: CPU usage percentage
    • %MEM: Memory usage percentage
    • COMMAND: Command name

The display automatically refreshes every 3 seconds by default. To exit the top command, simply press the q key.

Now, let's capture a static snapshot of the processes using the -n 1 option. This tells top to update only once and then exit:

top -n 1 > ~/project/top_snapshot.txt

This command creates a file named top_snapshot.txt in your project directory containing a snapshot of the current system processes. Let's verify the file contents:

cat ~/project/top_snapshot.txt

You should see output similar to what was displayed in the interactive top session, but as a static text file.

Interpreting the top Output

Now that you have a snapshot of the system processes, let's analyze the information to understand what it tells us about the system's resource usage.

Open the top_snapshot.txt file using the nano text editor:

nano ~/project/top_snapshot.txt

In this file, you can see the same information as displayed in the interactive top command. Let's focus on identifying the most CPU-intensive process from the list.

Look at the process list section (below the summary area) and find the process with the highest value in the %CPU column. This indicates the process that was consuming the most CPU at the time the snapshot was taken.

For example, if you see a line like this:

 1234 labex     20   0  562340  42340  28456 S   2.0   1.0   0:30.25 firefox

This shows that process with PID 1234 (firefox) owned by user "labex" was using 2.0% of CPU and 1.0% of memory.

Take note of the following information for the process with the highest CPU usage:

  1. PID (Process ID)
  2. USER (the owner of the process)
  3. %CPU (CPU usage percentage)
  4. %MEM (Memory usage percentage)
  5. COMMAND (The command or program running)

To exit nano, press Ctrl+X.

Now, create a file named top_analysis.txt to document your findings:

echo "Most CPU-intensive process analysis" > ~/project/top_analysis.txt

Add the details of the most CPU-intensive process to the file. Replace the placeholders with the actual values you observed:

echo "PID: [Replace with PID]" >> ~/project/top_analysis.txt
echo "USER: [Replace with USER]" >> ~/project/top_analysis.txt
echo "CPU%: [Replace with %CPU]" >> ~/project/top_analysis.txt
echo "MEM%: [Replace with %MEM]" >> ~/project/top_analysis.txt
echo "COMMAND: [Replace with COMMAND]" >> ~/project/top_analysis.txt

For example, if process 1234 (firefox) was the most CPU-intensive, you would enter:

echo "PID: 1234" >> ~/project/top_analysis.txt
echo "USER: labex" >> ~/project/top_analysis.txt
echo "CPU%: 2.0" >> ~/project/top_analysis.txt
echo "MEM%: 1.0" >> ~/project/top_analysis.txt
echo "COMMAND: firefox" >> ~/project/top_analysis.txt

Let's verify what we've written to the file:

cat ~/project/top_analysis.txt

This should display the contents of your analysis file with the details of the most CPU-intensive process.

Sorting and Filtering Processes in top

The top command becomes even more powerful when you learn how to sort and filter processes. In this step, you'll learn how to customize the top display to focus on specific information.

First, let's run the interactive top command again:

top

While top is running, you can use keyboard shortcuts to modify its behavior:

  1. Press M (uppercase) to sort processes by memory usage
  2. Press P (uppercase) to sort processes by CPU usage (default)
  3. Press T (uppercase) to sort processes by time (how long they've been running)
  4. Press N (uppercase) to sort processes by PID (process ID)

Try each of these sorting options to see how the process list changes. When you're done experimenting, press q to exit top.

Now, let's create a snapshot of processes sorted by memory usage. We'll use the batch mode of top with specific options:

top -b -n 1 -o %MEM > ~/project/top_sorted_by_mem.txt

This command uses:

  • -b: Batch mode (non-interactive)
  • -n 1: Run only one iteration
  • -o %MEM: Sort by memory usage

Let's check the contents of this file:

cat ~/project/top_sorted_by_mem.txt

You should see the processes listed in order of memory usage, with the highest memory-consuming processes at the top.

Next, let's filter processes to show only those belonging to your user account:

top -b -n 1 -u labex > ~/project/top_user_filtered.txt

The -u labex option filters the output to show only processes owned by the user "labex". Let's examine this file:

cat ~/project/top_user_filtered.txt

This file should only show processes owned by the "labex" user.

Finally, let's combine sorting and filtering to create a more specific view:

top -b -n 1 -u labex -o %CPU > ~/project/top_custom.txt

This command shows only processes belonging to the user "labex" and sorts them by CPU usage. Let's check the result:

cat ~/project/top_custom.txt

These customization options make top a versatile tool for focusing on specific aspects of system performance and resource usage.

Summary

In this lab, you have learned how to use the top command to monitor and analyze processes in Linux. The skills you've acquired include:

  1. Running the top command to view a real-time display of system processes
  2. Understanding the information displayed in the top output, including CPU usage, memory usage, and process details
  3. Capturing snapshots of process information for analysis
  4. Identifying resource-intensive processes by examining CPU and memory usage
  5. Customizing the top display through sorting and filtering options

These skills are essential for system administration and performance monitoring in Linux environments. The top command is one of the most frequently used tools for quickly assessing system health and identifying potential performance issues.

By mastering the top command, you now have the ability to effectively monitor system resources, identify processes that may be causing performance problems, and gather the information needed to optimize system performance.