pgrep Command Guide for Finding Process IDs
pgrep lists the process IDs of everything matching a pattern — the search half of the pkill pair, and the safe way to see what a kill would hit.
What is pgrep, and Why Use It?
The pgrep command looks through running processes and prints the PIDs whose name (or, with -f, full command line) matches a pattern you supply. It is the read-only twin of pkill: same pattern language, same flags, but instead of signaling it prints — so you can rehearse a kill before committing to it, or grab PIDs to feed into a script.
Why Learn pgrep?
- Safe killing:
pgrep -a patternshows exactly whatpkill patternwould hit — no surprises. - Scriptable process lookup: “is this daemon running?” becomes
pgrep -x nginx >/dev/null && echo yes. - Feeds other commands:
kill $(pgrep -f worker)andpstree -p $(pgrep cron)build pipelines from PIDs. - One syntax, two tools: the pattern rules are shared with
pkill, so learning both is one lesson.
pgrepis the question “which processes match this?” answered in one command — the first step of every careful kill.
1. Basic Usage: Find by Name
# Print PIDs of every process named "sshd"
pgrep sshd
- Output is one PID per line.
- Exit status 0 if at least one match, 1 if none — handy in scripts.
See the full command lines, not just PIDs:
pgrep -a sshd
-alists each match asPID command-line— the format you want when previewing.
2. Match Modes: Name, Command Line, Exact
# Match the full command line (arguments included)
pgrep -f "python3 worker.py"
# Exact name match only — "sshd" will NOT match "sshd: session"
pgrep -x sshd
# Case-insensitive name match
pgrep -i FIREFOX
| Flag | What It Matches |
|---|---|
| (none) | Process name, substring |
-f | Full command line |
-x | Exact name (anchored) |
-i | Case-insensitive |
-v | Invert — everything that does NOT match |
The difference between plain and -x matters: pgrep ssh matches sshd, sshd-session, and anything else containing “ssh”. pgrep -x sshd matches only the exact name.
3. Narrow by User, Terminal, or Parent
# Only processes owned by "bob"
pgrep -u bob
# Only your own processes
pgrep -u "$(id -un)"
# Only processes on a given terminal
pgrep -t pts/0
# Only children of PID 1 (init/systemd's direct children)
pgrep -P 1
-u,-t, and-Pcombine with name patterns for surgical searches.
4. Use PIDs in Pipelines and Scripts
# Kill everything matching, after previewing
pgrep -a -f worker
kill $(pgrep -f worker)
# Check if a service is running
if pgrep -x nginx > /dev/null; then
echo "nginx is running"
fi
# Count how many workers are alive
pgrep -c -f "worker.py"
-cprints a count instead of the PIDs — perfect for “is it up?” checks.- Redirecting to
/dev/nullturnspgrepinto a boolean test.
# Feed PIDs to ps for a formatted report
ps -p "$(pgrep -d, -f cron)" -o pid,ppid,cmd
-d,joins the PIDs with commas — the formatps -paccepts.
5. pgrep vs. ps and grep
# The old way: ps + grep (still works, more typing)
ps aux | grep sshd | grep -v grep
# The pgrep way
pgrep -a sshd
ps aux | grep nameworks but shows thegrepprocess itself and needs filtering;pgrepdoes the matching natively.- Use
pswhen you want the full table (CPU, memory, state); usepgrepwhen you only need PIDs.
Common Pitfalls
- Forgetting
-a: barepgrepgives bare PIDs with no context —-ashows what you actually found. - Substring surprises: plain
pgrepmatches substrings of names; use-xwhen you mean the exact program. -fcan match your own script: a pattern that appears in thepgrepcommand line itself will match — exclude it or anchor with-x.- Exit codes trip up scripts:
pgrepreturns 1 when nothing matches; wrap it inifor|| trueso a script does not abort. - Race conditions: a process can start or die between
pgrepand the next command — fine for interactive use, but scripts should handle “gone by the time I acted”.
Related Pages
| Page | Relationship |
|---|---|
pkill | The kill half — same patterns, signals instead of PIDs. |
ps | The full process table; pgrep is the quick lookup. |
kill | Consumes the PIDs pgrep prints. |
top | Live view for when you need more than PIDs. |
killall | Kills by exact name without patterns. |
pgrep Cheatsheet
| Command | What It Does |
|---|---|
pgrep name | Print PIDs matching name. |
pgrep -a name | Print PIDs with full command lines. |
pgrep -x name | Exact name match only. |
pgrep -f pattern | Match the full command line. |
pgrep -u user | Match only user’s processes. |
pgrep -P pid | Match children of pid. |
pgrep -c pat | Print the count of matches. |
pgrep -d, pat | Join PIDs with commas. |
kill $(pgrep pat) | Kill everything matching. |