pgrep
Find processes by name
TLDR
Return PIDs of any running processes with a matching command string
Search for processes including their command-line options
Search for processes run by a specific user
SYNOPSIS
pgrep [options] pattern
PARAMETERS
-c
Count the number of matching processes.
-d
Specify a delimiter to separate the PIDs in the output (default: newline).
-e
Match on the command name, instead of the process name.
-f
Match against the complete command line, not just the process name.
-g
Match processes belonging to the specified process group ID(s).
-G
Match processes belonging to the specified real group ID(s).
-i
Ignore case distinctions in the pattern.
-l
List the process name along with the PID.
-n
Select only the newest (most recently started) matching process.
-o
Select only the oldest matching process.
-P
Match processes with the specified parent process ID(s).
-s
Match processes with the specified session ID(s).
-t
Match processes attached to the specified terminal(s).
-u
Match processes belonging to the specified effective user ID(s).
-U
Match processes belonging to the specified user ID(s).
-v
Negate the match; select processes that do NOT match the pattern.
-x
Match only processes whose names match the pattern exactly (full match).
DESCRIPTION
pgrep searches the currently running processes and lists the process IDs (PIDs) that match the specified criteria.
It's primarily used for scripting and automation where you need to programmatically identify and potentially interact with processes. pgrep uses pattern matching against process names and other process attributes to narrow down the search. pgrep simplifies the task of identifying the PIDs required for other operations such as signaling (killing) or monitoring processes.
By default, pgrep returns all matching process IDs separated by newline characters. You can refine the search using different options to match specific users, full command lines, or processes based on their session ID.
It's a crucial tool for system administrators and developers alike for debugging and managing running applications.
CAVEATS
The pattern is interpreted as an extended regular expression. Be mindful of special characters in regular expressions. Always use quotes around the pattern if it contains special characters.
EXIT STATUS
0 if at least one process matched, 1 if no processes matched, and other values indicate an error.
HISTORY
pgrep is a relatively modern utility designed to simplify process identification.
It was developed as a more user-friendly alternative to combining tools like ps and grep for finding processes.
Its widespread adoption stems from its ease of use and script-friendliness.