How to check if a process is running in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn fundamental Linux skills for managing and monitoring processes. You will explore how to list all running processes on your system using the ps aux command, which provides a snapshot of current processes with detailed information like user, PID, CPU and memory usage.

Furthermore, you will discover how to efficiently find specific processes using the pgrep command and gain insights into real-time process activity and resource consumption by utilizing the top command. These techniques are essential for system administration, troubleshooting, and understanding system performance.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/ProcessManagementandControlGroup(["Process Management and Control"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux/ProcessManagementandControlGroup -.-> linux/kill("Process Terminating") linux/ProcessManagementandControlGroup -.-> linux/pkill("Pattern-Based Killing") linux/SystemInformationandMonitoringGroup -.-> linux/ps("Process Displaying") linux/SystemInformationandMonitoringGroup -.-> linux/top("Task Displaying") subgraph Lab Skills linux/kill -.-> lab-558747{{"How to check if a process is running in Linux"}} linux/pkill -.-> lab-558747{{"How to check if a process is running in Linux"}} linux/ps -.-> lab-558747{{"How to check if a process is running in Linux"}} linux/top -.-> lab-558747{{"How to check if a process is running in Linux"}} end

List running processes with ps aux

In this step, you will learn how to view the processes currently running on your Linux system using the ps command. Processes are simply programs that are running. Understanding how to list and identify processes is a fundamental skill in Linux system administration and troubleshooting.

The ps command is used to report a snapshot of the current processes. When used with certain options, it can provide a lot of detailed information.

Let's use the ps command with the aux options. These options are commonly used together to display all processes running on the system in a user-friendly format.

  • a: Displays all processes.
  • u: Displays the process's user and owner.
  • x: Displays processes that do not have a controlling terminal (useful for seeing background processes).

Open your terminal if it's not already open. You should be in the ~/project directory by default.

Type the following command and press Enter:

ps aux

You will see a long list of processes, similar to this (the exact output will vary):

USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.1  16840  9880 ?        Ss   Jul23   0:01 /sbin/init
root           2  0.0  0.0      0     0 ?        S    Jul23   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<   Jul23   0:00 [rcu_gp]
...
labex      12345  0.1  0.5 123456 54321 pts/0    Ss   HH:MM   0:00 /usr/bin/zsh
labex      12367  0.0  0.1  21000  5000 pts/0    R+   HH:MM   0:00 ps aux
...

Let's break down the columns you see:

  • USER: The user who owns the process.
  • PID: The Process ID, a unique number for each running process.
  • %CPU: The percentage of CPU time the process is using.
  • %MEM: The percentage of physical memory the process is using.
  • VSZ: Virtual Set Size, the total amount of virtual memory used by the process.
  • RSS: Resident Set Size, the amount of physical memory used by the process.
  • TTY: The controlling terminal for the process (if any). ? means no controlling terminal.
  • STAT: The process status (e.g., R for running, S for sleeping, Z for zombie).
  • START: The time the process started.
  • TIME: The cumulative CPU time the process has used.
  • COMMAND: The command that started the process.

Scrolling through this output can be overwhelming. In the next step, you'll learn how to filter this output to find specific processes.

Click Continue to proceed.

Find specific process using pgrep

In the previous step, you saw that ps aux can produce a lot of output. Often, you only want to find a specific process. The pgrep command is designed for this purpose. It searches for processes based on their name and returns their Process IDs (PIDs).

The basic syntax for pgrep is pgrep [options] pattern. The pattern is usually the name of the process you are looking for.

Let's try to find the PID of the zsh process, which is the shell you are currently using in the terminal.

Type the following command and press Enter:

pgrep zsh

You should see one or more numbers printed to the terminal. These are the PIDs of the running zsh processes.

12345

(The exact number will be different for you).

If you want to see the full command associated with the PID found by pgrep, you can combine it with the ps command. The -p option of ps allows you to specify a PID.

Let's find the full command for the zsh process using its PID. First, run pgrep zsh again to get the current PID. Let's assume the PID is 12345 for this example (replace 12345 with the actual PID you get).

Now, use ps -p followed by the PID:

ps -p 12345

You will see output similar to this:

    PID TTY          TIME CMD
  12345 pts/0    00:00:00 zsh

This confirms that the PID 12345 corresponds to the zsh command.

pgrep is a quick way to find the PID of a process when you know its name. This is particularly useful when you need the PID for other commands, like killing a process (which you'll learn about in a future lab).

Click Continue to move to the next step.

Monitor process details with top command

In this step, you will learn about the top command, which provides a dynamic, real-time view of the processes running on your system. While ps aux gives you a snapshot, top continuously updates, showing you which processes are using the most CPU and memory.

The top command is very useful for monitoring system performance and identifying processes that might be consuming excessive resources.

Type the following command in your terminal and press Enter:

top

Your terminal will change to display a constantly updating list of processes. The output looks similar to this:

top - HH:MM:SS up  X days, HH:MM,  X users,  load average: X.XX, X.XX, X.XX
Tasks: XXX total,   X running, XXX sleeping,   X stopped,   X zombie
%Cpu(s):  X.X us,  X.X sy,  X.X ni, XX.X id,  X.X wa,  X.X hi,  X.X si,  X.X st
MiB Mem :  XXXX.X total,  XXXX.X free,  XXXX.X used,  XXX.X buff/cache
MiB Swap:  XXXX.X total,  XXXX.X free,  XXXX.X used. XXXX.X avail Mem

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
  12345 labex     20   0  123456  54321  32100 S   0.1   0.5   0:00.10 zsh
  12367 labex     20   0   21000   5000   3000 R   0.0   0.1   0:00.05 top
  ...

Let's look at the key areas of the top output:

  • Summary Area (Top): This section shows system summary information, including:

    • Current time and system uptime.
    • Number of logged-in users.
    • Load average (average number of processes waiting to run over the last 1, 5, and 15 minutes).
    • Total number of tasks (processes) and their states (running, sleeping, stopped, zombie).
    • CPU usage breakdown (user, system, idle, etc.).
    • Memory usage (total, free, used, buffer/cache).
    • Swap space usage.
  • Process List Area (Bottom): This section lists individual processes, sorted by default by CPU usage. The columns are similar to ps aux, but top updates them in real-time. Key columns include:

    • PID: Process ID.
    • USER: Owner of the process.
    • %CPU: CPU usage percentage.
    • %MEM: Memory usage percentage.
    • COMMAND: The command name.

While top is running, you can interact with it using various keys:

  • Press q to quit top.
  • Press M to sort the process list by memory usage.
  • Press P to sort the process list by CPU usage (this is the default).
  • Press k to kill a process (you will be prompted for the PID). Be careful with this!

Spend a moment observing the processes and how the CPU and memory usage changes. You'll see the top process itself near the top of the list because it's actively using CPU to update the display.

When you are finished exploring top, press q to exit and return to your regular terminal prompt.

Click Continue to complete this step and the lab.

Summary

In this lab, you learned fundamental techniques for checking if a process is running in Linux. You started by using the ps aux command to list all running processes on the system, understanding the various columns like USER, PID, %CPU, and %MEM which provide a snapshot of process activity.

You then explored how to efficiently find a specific process using the pgrep command, which searches for processes based on their name or other attributes and returns their Process ID (PID). Finally, you learned how to monitor process details in real-time using the top command, which provides a dynamic view of system resource usage and individual process statistics, allowing you to observe CPU and memory consumption.