Linux pgrep Command

Complete Guide to Linux Commands


pgrep is a very practical process lookup tool in Linux systems, and it is part of the procps or procps-ng package. The name of this command comes from "process grep". As its name suggests, it can search for processes in the system just like the grep command searches text.

The main function of pgrep is to find running processes based on given conditions and return the PIDs (process IDs) of these processes. Compared to the traditional approach of combining the ps command with grep, pgrep is more concise and efficient.


pgrep Basic Syntax

The basic syntax format of the pgrep command is as follows:

pgrep [选项] [匹配模式]

Where:

  • Options: various options used to specify search conditions or output format
  • Match Mode: regular expression pattern used to match process names

Common Options

pgrep provides multiple options to precisely control search behavior. Here are the common options:

Option Description
-l Display both process name and PID
-a Display the full command line, not just the process name
-f Match the full command line (including arguments), not just the process name
-u Only match processes owned by the specified user
-x Exactly match the entire process name
-n Only display the newest (most recently started) matching process
-o Only display the oldest (earliest started) matching process
-c Only return the count of matching processes without displaying PIDs
-d Specify the output delimiter (default is newline)
-P Only match child processes of the specified parent process ID
-g Only match processes with the specified process group ID
-t Only match processes on the specified terminal (tty)

Usage Examples

Basic Process Search

Find all processes named "nginx":

pgrep nginx

This command returns the process IDs of all processes whose name contains "nginx".

Display Process Name

Find the "ssh" process and display its process name:

pgrep -l ssh

Example output:

1234 sshd
5678 ssh-agent

Exact Match Process Name

Only match processes exactly named "bash" (excluding "bashrc" and similar):

pgrep -x bash

Match Full Command Line

Find processes containing specific arguments, for example, find sshd processes using a specific port:

pgrep -f "sshd -p 2222"

Filter by User

Find all processes run by user "www-data":

pgrep -u www-data

Combined Condition Search

Find processes named "mysqld" run by user "mysql":

pgrep -u mysql mysqld

Count Processes

Count the number of running "chrome" processes:

pgrep -c chrome

Find Newest/Oldest Process

Find the most recently started "python" process:

pgrep -n python

Find the earliest started "python" process:

pgrep -o python

Advanced Usage

Using with pkill

pgrep is often used in conjunction with the pkill command. You can first use pgrep to view the processes to be terminated, and after confirming they are correct, use pkill:

Example

# First view the matching processes
pgrep -l apache2

# Terminate these processes after confirmation
pkill apache2

Custom Delimiter

By default, pgrep uses newlines to separate multiple PIDs. You can change it to use commas:

pgrep -d, nginx

Example output:

1234,5678,9012

Find Processes on Specific Terminal

Find all processes running on the tty1 terminal:

pgrep -t tty1

Find Child Processes

Find all child processes of the process with PID 1234:

pgrep -P 1234

FAQ

What is the difference between pgrep and ps | grep?

  1. Efficiency: pgrep directly queries kernel process information, making it more efficient than ps | grep
  2. Safety: pgrep does not match its own process, while ps | grep may match the grep process itself
  3. Features: pgrep provides more specialized process search options

Why does pgrep sometimes fail to find a process I know is running?

Possible reasons:

  1. The process name is misspelled
  2. Not using the-foption to match the full command line
  3. The process belongs to another user, and you do not have sufficient permission to view it
  4. The process has already terminated

How to check the pgrep version?

pgrep -V

Practical Use Cases

Monitor Service Status

Check whether a service is running in a script:

Example

if pgrep -x "nginx" >/dev/null; then
    echo "Nginx is running"
else
    echo "Nginx is not running"
fi

Batch Operations on Processes

Get the PIDs of all Java processes and perform operations:

Example

for pid in $(pgrep java); do
    echo "Processing Java process $pid"
    # Other operations...
done

System Resource Monitoring

Count the number and resource usage of specific types of processes:

Example

count=$(pgrep -c chrome)
memory=$(ps -o rss= -p $(pgrep chrome) | awk '{sum+=$1} END {print sum}')
echo "Chrome processes: $count, Total memory: ${memory}KB"

Summary

pgrep is a powerful and efficient tool in Linux system administration. It simplifies process lookup and management operations. By mastering the various options and usages of pgrep, you can:

  1. Quickly and accurately locate specific processes
  2. Write more reliable system administration scripts
  3. Improve the efficiency of daily system maintenance work

Remember, after using pgrep to find processes, especially when preparing to terminate processes, always confirm that the processes found are the correct ones to avoid accidentally terminating important services.


Complete Guide to Linux Commands

Other Extensions