🔍 Pgrep Command Guide

Process Searching Made Easy - 10 Practical Examples

What is Pgrep?

pgrep is a command-line utility that searches for processes based on name and other attributes, returning process IDs (PIDs). It's part of the procps-ng package and provides a simpler alternative to combining ps and grep commands. It's especially useful in scripts and for quickly finding process IDs for monitoring or management tasks.

Example 1

Basic Process Search

pgrep firefox
1234 1235 1238
Purpose: Find the process ID(s) of a running process by name.
Details: Returns all PIDs that match the process name "firefox".
Output: Each line shows one matching process ID.
Traditional Alternative: ps aux | grep firefox | grep -v grep
Pgrep is much simpler and cleaner!
Example 2

List Full Command with Process Name

pgrep -l ssh
1045 sshd 2341 ssh-agent 3892 sshd
Purpose: Display both PID and process name.
Flag: -l (list) shows process names alongside PIDs.
Use Case: Quickly verify which processes match your search without needing additional commands.
Tip: Use -a flag to show the full command line instead of just the process name.
Example 3

Search by User

pgrep -u john firefox
5678 5679
Purpose: Find processes owned by a specific user.
Flag: -u john filters processes owned by user "john".
Use Cases:
  • Find all Firefox instances running under a specific user account
  • Useful in multi-user systems
  • System administration and monitoring
Multiple Users: pgrep -u john,mary firefox searches for processes owned by john OR mary.
Example 4

Show Full Command Line

pgrep -a python
1234 python /home/user/script.py 2345 python -m http.server 8000 3456 python /opt/app/main.py --debug
Purpose: Display complete command line arguments.
Flag: -a shows the full command line with all arguments.
Benefit: Helps distinguish between multiple instances of the same program running with different arguments.
Note: This is extremely useful for identifying which specific Python script or Java application is running.
Example 5

Count Matching Processes

pgrep -c nginx
4
Purpose: Count the number of matching processes.
Flag: -c returns a count instead of PIDs.
Use Cases:
  • Monitor process pools (web servers, databases)
  • Verify expected number of worker processes
  • Scripting and automation checks
  • Health monitoring systems
Script Usage: Perfect for if-statements: if [ $(pgrep -c nginx) -lt 4 ]; then ...
Example 6

Get Newest/Oldest Process

# Get newest process
pgrep -n chrome

# Get oldest process
pgrep -o chrome
# Newest:
9876

# Oldest:
1234
Purpose: Find the most recently or first started process.
Flags:
  • -n: Returns the newest (most recently started) matching process
  • -o: Returns the oldest (first started) matching process
Use Case: Target a specific instance when multiple processes exist, especially useful for killing parent processes.
Example 7

Exact Match Search

pgrep -x bash
1001 1234 3456
Purpose: Match the exact process name only.
Flag: -x requires exact match (whole string).
Difference:
  • pgrep bash matches "bash", "bashrc-script", "my-bash-tool"
  • pgrep -x bash matches only "bash"
Best Practice: Use -x when you need precise matching to avoid false positives.
Example 8

Search by Parent Process ID (PPID)

pgrep -P 1000
1001 1002 1005
Purpose: Find all child processes of a specific parent.
Flag: -P 1000 finds all processes whose parent PID is 1000.
Use Cases:
  • Track all subprocesses spawned by a main application
  • Manage process trees
  • Clean up orphaned processes
  • Debugging parent-child process relationships
Combined Example: pgrep -P $(pgrep -n nginx) finds children of the newest nginx process.
Example 9

Search by Terminal

pgrep -t pts/1
2345 2346 2350
Purpose: Find processes associated with a specific terminal.
Flag: -t pts/1 shows processes on terminal pts/1.
Details: Useful for finding which processes are running in which terminal session.
Check Terminal: Use tty command to see your current terminal name.
Use Case: Identify and manage processes in specific SSH sessions or terminal windows.
Example 10

Inverse Match (Exclude User)

pgrep -v -u root
1234 1235 2341 5678 6789
Purpose: Find processes NOT matching the criteria.
Flag: -v inverts the match (similar to grep -v).
Example: pgrep -v -u root shows all processes NOT owned by root.
Use Cases:
  • Find user processes (exclude system processes)
  • Security auditing
  • Resource monitoring for non-privileged processes
Bonus

Complex Search with Multiple Criteria

pgrep -u john -a -l python | grep -v test
3456 python /home/john/app/server.py 5678 python /home/john/scripts/backup.py
Purpose: Combine multiple filters for precise process searches.
Combined Flags:
  • -u john: Only user john's processes
  • -a: Show full command line
  • -l: Include process names
  • Piped to grep to further filter results
Result: All Python processes by user john, excluding those with "test" in the command line.
Practical Script

Using Pgrep in Scripts

#!/bin/bash
# Check if nginx is running
if pgrep -x nginx > /dev/null; then
echo "Nginx is running"
else
echo "Nginx is not running"
systemctl start nginx
fi

# Kill all Chrome processes by user
pgrep -u $USER chrome | xargs kill

# Monitor process count
COUNT=$(pgrep -c httpd)
if [ $COUNT -lt 5 ]; then
echo "Warning: Only $COUNT httpd processes running"
fi
Common Scripting Patterns:
  • Process existence checks
  • Automated process management
  • Health monitoring and alerting
  • Resource cleanup
Note: Always be careful when piping pgrep to kill commands. Test thoroughly!