| title | Linux Command Tutorial: pgrep | ||||
|---|---|---|---|---|---|
| date | 2026-09-12 00:00:00 +0000 | ||||
| categories |
|
||||
| tags |
|
||||
| draft | false | ||||
| slug | linux-pgrep-tutorial | ||||
| description | Authoritative reference tutorial for pgrep (procps-ng), detailing process searching by pattern, user filtering (-u), exact name matching (-x), and full command matching (-f). | ||||
| upstream_suite | procps-ng | ||||
| upstream_version | procps-ng 4.0.4 | ||||
| posix_standard | None | ||||
| research_date | 2026-09-12 |
The Linux Command Tutorial series provides rigorous, upstream-verified references for essential system commands across Linux distributions and UNIX-like environments. Each article focuses on a single executable, combining exhaustive option documentation, verified real-world examples, security boundaries, and best practices directly derived from official source documentation and POSIX standards.
Upstream: procps-ng 4.0.4 | POSIX: De-facto Standard (Not POSIX standardized) | Safety Tier: safe-read-only | Scope: process-search
pgrep searches the currently running processes and outputs the process IDs (PIDs) matching specified selection criteria to standard output. It eliminates the need for fragile pipelines like ps aux | grep name | awk '{print $2}'.
- Upstream Project & Provenance: Maintained within procps-ng (
procps-ng). - Portability & Standards Baseline:
pgrepis an industry-standard UNIX utility originating from Solaris; not defined in POSIX.1-2024. - Target Research Implementation: Audited against procps-ng 4.0.4 (
pgrep(1)). - Applicability & Lifecycle: The preferred tool for finding process IDs based on executable names, arguments, usernames, or session groups.
pgrep [options] patternpatternis evaluated as an Extended Regular Expression (ERE).- By default,
pgrepmatches against the basename of the executable (from/proc/[pid]/stat). - If
-f(full) is passed,pgrepmatches against the complete command line and arguments (from/proc/[pid]/cmdline). - Output consists of matching PIDs separated by newlines (or custom delimiter via
-d).
| Flag | Description | Default | Upstream Note |
|---|---|---|---|
-l |
List the process name as well as the process ID. | PID only | Convenient for verification |
-a |
List the full command line arguments as well as the PID. | PID only | Avoids ambiguity |
-f |
Match pattern against full command line arguments, not just basename. | Basename | Crucial for scripts |
-x |
Exact match: require pattern to match the entire name or command line. | Substring | Prevents false positives |
-u user |
Match only processes owned by effective user ID or username. | All users | User filtering |
-U user |
Match only processes owned by real user ID or username. | All users | User filtering |
-o |
Select only the oldest (least recently started) matching process. | All matches | Singleton selection |
-n |
Select only the newest (most recently started) matching process. | All matches | Singleton selection |
-c |
Suppress normal output; print a count of matching processes. | Print PIDs | Counting instances |
-d delim |
Set string used to separate PIDs in output (e.g. ,). |
Newline \n |
Pipeline friendly |
| Operation | Command | Notes |
|---|---|---|
| Find PIDs by name | pgrep nginx |
Substring match against binary name |
| Exact name match | pgrep -x sshd |
Avoids substring false positives |
| List PID and command name | pgrep -l nginx |
Prints PID and short binary name |
| Match full command line | pgrep -fa "app.py" |
Inspects argument vector and prints full command |
| Filter by username | pgrep -u www-data php-fpm |
Restricts search to user's processes |
| Count matching processes | pgrep -c nginx |
Returns numeric count of matches |
| Comma-delimited list | pgrep -d, worker |
Generates PID list for top -p or kill |
pgrep nginx4512
4513
4514
pgrep -l sshd1105 sshd
4912 sshd
When searching for a service named sh, standard pgrep sh matches sshd, bash, ssh-agent, etc.:
pgrep -x sh- Technical Analysis:
-xrequires the pattern to match the entire process name from start to finish (^pattern$), matching strictly/bin/shprocesses.
Because Python processes appear in /proc as python3, searching for worker.py with standard pgrep matches nothing:
pgrep -fa "worker.py"12402 python3 /opt/app/worker.py --concurrency=4
-finspects the full argument vector, while-aprints the arguments for visual confirmation.
Checking how many worker processes are currently alive:
pgrep -c -u www-data php-fpm16
Passing PIDs directly into tools like top or strace:
top -p $(pgrep -d, nginx)Tip
No Self-Matching: Unlike ps aux | grep nginx (which matches the running grep process itself unless filtered), pgrep automatically excludes its own PID from search results.
| Exit Code | Meaning |
|---|---|
0 |
One or more matching processes were found. |
1 |
No matching processes were found. |
2 |
Syntax error in the command-line options. |
3 |
Fatal error (e.g. out of memory). |
This makes pgrep ideal for shell conditional tests:
if pgrep -x nginx > /dev/null; then
echo "Nginx is running"
fiNote
Linux limits the kernel task comm field (/proc/[pid]/comm) to 15 characters. If an executable name exceeds 15 characters, standard pgrep matches against the truncated name. Pass -f to match against the full command line from /proc/[pid]/cmdline.
- Always Use
-xWhen Matching Standard Utilities:- Guidance: Write
pgrep -x <name>when matching well-known binaries. - Authoritative Justification: Prevents substring collisions with unrelated daemons.
- Guidance: Write
- Use
-ffor Interpreted Scripts (Python, Node, Java):- Guidance: Pass
-fwhen searching for script paths. - Authoritative Justification: The binary name of interpreted scripts is the interpreter (
python3), not the script name.
- Guidance: Pass
- Use
-cfor Instance Monitoring:- Guidance: Check pool concurrency using
pgrep -c. - Authoritative Justification: Avoids piping into
wc -l.
- Guidance: Check pool concurrency using
- procps-ng pgrep(1) Manual: https://man7.org/linux/man-pages/man1/pgrep.1.html
- Linux /proc/[pid]/status Documentation: https://docs.kernel.org/filesystems/proc.html