pgrep(1)
Search for processes by name and print their process IDs.
Synopsis
pgrep [OPTION]... PATTERNDescription
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
| Flag | What it does |
|---|---|
-l | List process name along with the PID |
-f | Match against full command line, not just process name |
-u USERNAME | Only match processes owned by a specific user |
-U USERNAME | Only match processes whose real UID is specified user |
-g PGID | Only match processes in specified process group |
-P PPID | Only match child processes of specified parent PID |
-x | Match the entire process name exactly (not partial match) |
-v | Invert match; select non-matching processes |
-n | Return only the newest (most recently started) process |
-o | Return only the oldest (least recently started) process |
-c | Suppress output and return count of matching processes |
-a | Include process name in output (used with -l) |
Examples
Find all PIDs of processes named nginx
pgrep nginxShow PID and process name of apache2 processes
pgrep -l apache2Find sshd processes owned by root
pgrep -u root sshdMatch 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 chromeCount how many bash processes are running
pgrep -c bashFind Firefox processes that are direct children of current shell
pgrep -P $$ firefoxFind processes with exact name 'java' (not 'java-config', etc.)
pgrep -x java