Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 pattern shows exactly what pkill pattern would 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) and pstree -p $(pgrep cron) build pipelines from PIDs.
  • One syntax, two tools: the pattern rules are shared with pkill, so learning both is one lesson.

pgrep is 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
  • -a lists each match as PID 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
FlagWhat It Matches
(none)Process name, substring
-fFull command line
-xExact name (anchored)
-iCase-insensitive
-vInvert — 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 -P combine 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"
  • -c prints a count instead of the PIDs — perfect for “is it up?” checks.
  • Redirecting to /dev/null turns pgrep into 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 format ps -p accepts.

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 name works but shows the grep process itself and needs filtering; pgrep does the matching natively.
  • Use ps when you want the full table (CPU, memory, state); use pgrep when you only need PIDs.

Common Pitfalls

  • Forgetting -a: bare pgrep gives bare PIDs with no context — -a shows what you actually found.
  • Substring surprises: plain pgrep matches substrings of names; use -x when you mean the exact program.
  • -f can match your own script: a pattern that appears in the pgrep command line itself will match — exclude it or anchor with -x.
  • Exit codes trip up scripts: pgrep returns 1 when nothing matches; wrap it in if or || true so a script does not abort.
  • Race conditions: a process can start or die between pgrep and the next command — fine for interactive use, but scripts should handle “gone by the time I acted”.

PageRelationship
pkillThe kill half — same patterns, signals instead of PIDs.
psThe full process table; pgrep is the quick lookup.
killConsumes the PIDs pgrep prints.
topLive view for when you need more than PIDs.
killallKills by exact name without patterns.

pgrep Cheatsheet

CommandWhat It Does
pgrep namePrint PIDs matching name.
pgrep -a namePrint PIDs with full command lines.
pgrep -x nameExact name match only.
pgrep -f patternMatch the full command line.
pgrep -u userMatch only user’s processes.
pgrep -P pidMatch children of pid.
pgrep -c patPrint the count of matches.
pgrep -d, patJoin PIDs with commas.
kill $(pgrep pat)Kill everything matching.