Linux Process Management

LinuxLinuxBeginner
Practice Now

Introduction

This lab introduces essential Linux tools for managing processes and system resources. As a beginner, you'll learn how to use ps, top, and free commands to monitor and analyze system processes and memory usage. These tools are fundamental for understanding what's happening on your Linux system.

Achievements

By the end of this lab, you'll be able to:

  • List and filter processes using ps
  • Monitor processes in real-time with top
  • Check system memory usage using free

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/SystemInformationandMonitoringGroup -.-> linux/ps("`Process Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/top("`Task Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/free("`Memory Reporting`") subgraph Lab Skills linux/ps -.-> lab-46{{"`Linux Process Management`"}} linux/top -.-> lab-46{{"`Linux Process Management`"}} linux/free -.-> lab-46{{"`Linux Process Management`"}} end

List All Processes

Let's start by listing all processes running on the system using the ps command.

  1. Open your terminal. You should be in the /home/labex/project directory. If you're not sure, you can always check your current directory by typing pwd and pressing Enter.

  2. Run the following command to list all processes:

    ps -e

    This will display a list of all processes, including their Process ID (PID), terminal (TTY), CPU time, and command name.

  3. Take a moment to examine the output. You'll see system processes and user processes. Here's what each column means:

    • PID: The unique Process ID
    • TTY: The terminal type associated with the process
    • TIME: The amount of CPU time used by the process
    • CMD: The command or program name

    Don't worry if you see unfamiliar process names; many of these are system processes that run in the background.

Display Detailed Process Information

Now, let's get more detailed information about the processes.

  1. Run the following command:

    ps aux

    This command shows a detailed list of all processes, including the user who started the process, CPU and memory usage, and the full command line.

  2. The output might be quite long. You can use the less command to view it more easily:

    ps aux | less

    This pipes the output of ps aux into less, allowing you to scroll through it.

  3. In the less viewer:

    • Use the up and down arrow keys to scroll
    • Press Space to move forward one page
    • Press b to move back one page
    • Press q to exit the less viewer when you're done
  4. Let's break down what you're seeing:

    • USER: The owner of the process
    • PID: Process ID
    • %CPU: CPU usage
    • %MEM: Memory usage
    • VSZ: Virtual memory size
    • RSS: Resident Set Size (non-swapped physical memory used)
    • TTY: Terminal type
    • STAT: Process state
    • START: Start time of the process
    • TIME: Cumulative CPU time
    • COMMAND: The command with all its arguments

Don't worry if you don't understand all of these right now. As you gain more experience, you'll become familiar with these terms.

Filter Processes by User

You can filter processes to show only those belonging to a specific user. This is useful when you want to focus on processes owned by a particular user account.

  1. To see processes owned by the root user, run:

    ps -u root

    The root user is the superuser in Linux systems, with full system access rights. Many system processes run under the root account.

  2. To see processes owned by your current user (labex), run:

    ps -u labex

    This will show you all processes that you've started under your user account.

  3. Compare the outputs of these two commands. You'll likely see more processes under root than under your user account.

Monitor Processes in Real-time with top

The top command provides a dynamic, real-time view of the running processes. It's like a live dashboard for your system.

  1. Run the following command:

    top
  2. You'll see a continuously updating display of system processes, sorted by CPU usage. The display is divided into two main sections:

    • The summary area at the top, showing system-wide information
    • The process list, showing details of individual processes
  3. While in top, you can use various commands:

    • Press M to sort by memory usage instead of CPU
    • Press P to return to sorting by CPU usage
    • Press T to sort by running time
    • Press N to sort by PID
    • Use the up and down arrow keys to navigate the process list
    • Press q to quit top
  4. The summary area includes important system information:

    • System uptime and load averages
    • Tasks summary (total, running, sleeping, stopped, zombie)
    • CPU states (user, system, idle, etc.)
    • Memory usage (total, free, used, buffers/cached)
    • Swap usage

Take some time to explore top. It's a powerful tool for understanding what's happening on your system in real-time.

Check System Memory Usage

The free command allows you to check the system's memory usage quickly and easily.

  1. Run the following command to see memory usage in a human-readable format:

    free -h

    The -h option stands for "human-readable", which displays the sizes in powers of 1024 (e.g., 1K, 234M, 2G).

  2. You'll see output similar to this:

                  total        used        free      shared  buff/cache   available
    Mem:           7.7G        4.1G        287M        624M        3.3G        2.7G
    Swap:          2.0G        506M        1.5G

    Let's break down what each row and column means:

    • Mem: This row shows information about your system's RAM
    • Swap: This row shows information about your swap space (virtual memory)

    Columns:

    • total: Total installed memory
    • used: Memory currently in use
    • free: Unused memory
    • shared: Memory shared by multiple processes
    • buff/cache: Memory used by kernel buffers, page cache, and slabs
    • available: Estimate of how much memory is available for starting new applications, without swapping
  3. Don't be alarmed if your "free" memory seems low. Linux uses available memory for disk caching to improve system performance, but this memory can be reclaimed if needed by applications.

Summary

In this lab, you've learned how to use key Linux process management tools:

  • ps to list and filter processes
  • top to monitor processes in real-time
  • free to check system memory usage

These commands are essential for system administration and troubleshooting. They give you insight into what's running on your system, how resources are being used, and can help you identify potential issues.

As you become more comfortable with these tools, you can explore additional options and combine them with other Linux commands for more advanced system analysis. For example:

  • Use ps with grep to find specific processes
  • Learn to interpret the detailed information in top to identify resource-intensive applications
  • Use free in scripts to monitor memory usage over time

Remember, practice makes perfect! Don't hesitate to experiment with these commands on your own to deepen your understanding of Linux process management. The more you use these tools, the more intuitive they'll become.

Other Linux Tutorials you may like