Processes and tasks

Command pgrep

Find PID by process name

Linux · Bash examples · commands run on the selected server

When to use pgrep

Use pgrep to locate processes by names and other attributes before inspecting or signalling them. Choose exact-name matching or full-command matching deliberately; a broad pattern can select unrelated processes.

Syntax

pgrep [OPTIONS] TEMPLATE

Square brackets mark optional arguments; an ellipsis means repetition. Replace example file names, user names and server addresses with your own values.

Common options and arguments

-a
Show PID and command line.
-x
Exact process name match.
-u NAME
Select an effective user.

Usage examples

Find processes nginx

pgrep -a -x nginx

Exact-name nginx matches are displayed with their command lines.

View your shells

pgrep -a -u "$USER" bash

Matching bash processes are restricted to the current user's account.

Search in full command line

pgrep -af 'python.*worker'

A regular expression is matched against full command lines, which is broader than executable-name matching.

Notes and limitations

  • By default, the pattern is a regular expression for the process name; -f uses the full command line.
  • Code 1 means no match; the process may end immediately after the search.

Common questions and troubleshooting

Why are more processes matched than expected?

Full-command matching includes arguments and regular expressions. Narrow the account and pattern and review the output before acting.

What does no output mean?

There may be no matches, or visibility can be limited. Check the exit status and permissions instead of treating an empty result as a complete system inventory.

Further help

Options vary by version and distribution. Check the installed documentation with this command: man pgrep.