Linux pgrep Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux pgrep command to search for running processes based on their name or other criteria. The pgrep command is a powerful tool that can be used in various scenarios, such as monitoring system processes, automating tasks, or troubleshooting issues. You will start by understanding the purpose of the pgrep command, then learn how to use it to search for processes by name, and finally explore how to combine pgrep with other commands for advanced process monitoring.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux(("`Linux`")) -.-> linux/ProcessManagementandControlGroup(["`Process Management and Control`"]) linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") linux/SystemInformationandMonitoringGroup -.-> linux/ps("`Process Displaying`") linux/ProcessManagementandControlGroup -.-> linux/kill("`Process Terminating`") subgraph Lab Skills linux/grep -.-> lab-422855{{"`Linux pgrep Command with Practical Examples`"}} linux/ps -.-> lab-422855{{"`Linux pgrep Command with Practical Examples`"}} linux/kill -.-> lab-422855{{"`Linux pgrep Command with Practical Examples`"}} end

Understand the Purpose of pgrep Command

In this step, you will learn about the purpose and usage of the pgrep command in Linux. The pgrep command is a powerful tool that allows you to search for running processes based on their name or other criteria.

The pgrep command is useful when you need to find the process ID (PID) of a running process, especially when you don't know the exact process name or want to search for processes based on specific criteria. This can be helpful in various scenarios, such as monitoring system processes, automating tasks, or troubleshooting issues.

Let's start by exploring the basic usage of the pgrep command:

pgrep <process_name>

This command will return the PID(s) of the process(es) that match the specified <process_name>.

Example output:

1234
5678
9012

The output shows the PIDs of the processes that match the specified process name.

In this step, you will learn how to use the pgrep command to search for processes by their name.

The basic syntax for using pgrep to search for processes by name is:

pgrep <process_name>

This will return the PID(s) of the process(es) that match the specified <process_name>.

You can also use additional options with pgrep to refine your search. For example:

## Search for processes with a name that starts with "web"
pgrep -f ^web

## Search for processes owned by the "labex" user
pgrep -u labex

## Search for processes with a name that contains "nginx"
pgrep -l nginx

The -f option allows you to search for processes based on the full command line, not just the process name. The -u option lets you search for processes owned by a specific user, and the -l option prints the process name along with the PID.

Example output:

1234 webserver
5678 nginx
9012 webapplication

This output shows the PIDs and process names of the matching processes.

Combine pgrep with Other Commands for Advanced Process Monitoring

In this final step, you will learn how to combine the pgrep command with other Linux commands to perform more advanced process monitoring tasks.

One common use case is to combine pgrep with the ps command to get more detailed information about the running processes:

ps -f -p $(pgrep <process_name>)

This will display the full process information, including the user, PID, parent PID, start time, and command for the processes that match the specified <process_name>.

Example output:

UID        PID  PPID  C STIME TTY          TIME CMD
labex     1234  5678  0 10:30 pts/0    00:00:01 /usr/bin/python3 /app/webserver.py
labex     5678  9012  0 10:29 pts/0    00:00:00 nginx: master process nginx -g daemon off;
labex     9012  1234  0 10:29 pts/0    00:00:00 nginx: worker process

You can also use pgrep with other commands, such as kill or top, to manage or monitor processes more effectively:

## Kill all processes with a name that starts with "web"
sudo kill -9 $(pgrep -f ^web)

## Monitor the top CPU-consuming processes
top -p $(pgrep -d, <process_name>)

The first example uses pgrep to find the PIDs of all processes with a name starting with "web", and then uses the kill command to terminate them. The second example uses pgrep to get the PIDs of the processes matching the specified <process_name>, and then passes them to the top command to monitor their resource usage.

Summary

In this lab, you learned about the purpose and usage of the pgrep command in Linux. The pgrep command is a powerful tool that allows you to search for running processes based on their name or other criteria, which can be helpful in various scenarios, such as monitoring system processes, automating tasks, or troubleshooting issues. You also learned how to use pgrep to search for processes by their name, including using additional options to refine your search, such as searching for processes that start with a specific name, are owned by a specific user, or contain a specific string in their name.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like