Home·Terminal commands

pgrep

Processes and system

Usage

pgrep [-f] <pattern>

Prints just the PIDs a pattern matches, using exactly the same rules as pkill, which makes it the dry run to do before you kill anything.

Common flags

FlagMeaning
-fMatch the full command line, so arguments count too.
-aPrint the command line next to the PID (Linux).
-lPrint the process name next to the PID.
-xExact name match only.
-u <user>Restrict to one user.
-cPrint how many matched instead of the PIDs.
-n / -oNewest / oldest match only.

Examples

pgrep -af node

Every node process with its full command line.

pgrep -x sshd

The PID of sshd itself, not of anything merely mentioning ssh.

kill $(pgrep -f "celery worker")

Stop the processes the pattern found.

What is running and what is eating the machine. Identifying it comes before killing it.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does pgrep do?

Prints just the PIDs a pattern matches, using exactly the same rules as pkill, which makes it the dry run to do before you kill anything.

Q. How do I type it?

pgrep [-f] <pattern> — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

7 are listed here; the full set is in man pgrep. This command sits under Processes and system.

Related commands

man page: man pgrep