pgrep Command
Intermediate Process Management man(1)Look up processes based on name and attributes
👁 12 views
📅 Updated: Mar 15, 2026
SYNTAX
pgrep [OPTION]... PATTERN
What Does pgrep Do?
pgrep searches for processes by name or other criteria and outputs their PIDs. It is the lookup companion to pkill — pgrep finds processes, pkill kills them, using identical matching syntax.
pgrep replaces the common ps aux | grep pattern | grep -v grep idiom with a cleaner, more reliable command. It supports matching by process name, full command line, user, group, terminal, and more.
pgrep is essential in scripts for checking if a process is running, getting PIDs for further processing, and building process monitoring logic.
pgrep replaces the common ps aux | grep pattern | grep -v grep idiom with a cleaner, more reliable command. It supports matching by process name, full command line, user, group, terminal, and more.
pgrep is essential in scripts for checking if a process is running, getting PIDs for further processing, and building process monitoring logic.
Options & Flags
| Option | Description | Example |
|---|---|---|
| -a | Show PID and full command line | pgrep -a nginx |
| -f | Match against full command line | pgrep -f 'python3 app.py' |
| -u | Match by effective user | pgrep -u www-data |
| -c | Count matching processes | pgrep -c nginx |
| -l | Show PID and process name | pgrep -l ssh |
| -x | Exact match only | pgrep -x nginx |
| -n | Show only newest match | pgrep -n python3 |
| -o | Show only oldest match | pgrep -o python3 |
| -d | Set output delimiter | pgrep -d',' nginx |
Practical Examples
#1 Find process PIDs
Lists PIDs of all nginx processes.
$ pgrep nginx
Output:
1234
1235
1236
#2 Show PIDs with command lines
Shows PIDs and full command lines for all Python processes.
$ pgrep -a python
Output:
1234 python3 manage.py runserver
5678 python3 celery worker
#3 Count instances
Returns the count of running PHP-FPM processes.
$ pgrep -c php-fpm
Output:
8
#4 Check if running (scripting)
Checks if nginx is running for monitoring scripts.
$ pgrep -x nginx > /dev/null && echo "Running" || echo "Stopped"
Output:
Running
#5 Match full command line
Finds processes with "node" and "server.js" anywhere in the command line.
$ pgrep -af 'node.*server.js'#6 Get comma-separated PIDs
Returns PIDs as comma-separated list — useful for top -p.
$ pgrep -d',' php-fpm
Output:
1234,1235,1236
Tips & Best Practices
Better than ps | grep: pgrep -a nginx is cleaner and more reliable than ps aux | grep nginx | grep -v grep. It doesn't match itself and supports exact matching.
Exit codes: pgrep returns 0 if at least one match, 1 if no matches. Use in scripts: if pgrep -x nginx > /dev/null; then ...
Default matches process name only: Without -f, pgrep matches only the process name (like ps -C). Add -f to match full command line arguments.
Frequently Asked Questions
How do I check if a process is running?
Use pgrep -x process_name. It returns 0 if running. In scripts: pgrep -x nginx > /dev/null && echo 'Running'.
What is the difference between pgrep and pidof?
pgrep matches patterns and supports many filters (user, command line, etc.). pidof requires exact binary name. pgrep is more flexible.
How do I use pgrep output with other commands?
Use command substitution: kill $(pgrep nginx), or pipe: pgrep php-fpm | xargs kill. Or use pgrep -d',' for comma-separated PIDs.
Related Commands
More Process Management Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →