$linuxjunkies
>

pgrep(1)

Search for processes by name and print their process IDs.

UbuntuDebianFedoraArch

Synopsis

pgrep [OPTION]... PATTERN

Description

pgrep searches the process list on the running system and prints the process ID of each process whose name or command line matches the given PATTERN. It is useful for finding running processes without knowing their exact PID, and pairs well with other process manipulation commands.

By default, pgrep matches against the process name (the executable name). You can also match against the full command line with -f, or filter by user, group, or other attributes.

Common options

FlagWhat it does
-lList process name along with the PID
-fMatch against full command line, not just process name
-u USERNAMEOnly match processes owned by a specific user
-U USERNAMEOnly match processes whose real UID is specified user
-g PGIDOnly match processes in specified process group
-P PPIDOnly match child processes of specified parent PID
-xMatch the entire process name exactly (not partial match)
-vInvert match; select non-matching processes
-nReturn only the newest (most recently started) process
-oReturn only the oldest (least recently started) process
-cSuppress output and return count of matching processes
-aInclude process name in output (used with -l)

Examples

Find all PIDs of processes named nginx

pgrep nginx

Show PID and process name of apache2 processes

pgrep -l apache2

Find sshd processes owned by root

pgrep -u root sshd

Match against full command line to find specific Python script

pgrep -f 'python script.py'

Get PID of the most recently started Chrome process

pgrep -n chrome

Count how many bash processes are running

pgrep -c bash

Find Firefox processes that are direct children of current shell

pgrep -P $$ firefox

Find processes with exact name 'java' (not 'java-config', etc.)

pgrep -x java

Related commands