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
-l, --list-name
List the process name as well as the process ID.
-f, --full
The pattern is matched against the full command line (including arguments) instead of just the process name.
-i, --ignore-case
Ignore case distinctions in the pattern.
-u
Only match processes whose effective user ID is in the comma-separated uidlist.
-U
Only match processes whose real user ID is in the comma-separated uidlist.
-g
Only match processes whose effective group ID is in the comma-separated gidlist.
-G
Only match processes whose real group ID is in the comma-separated gidlist.
-P
Only match processes whose parent process ID is in the comma-separated ppidlist.
-t
Only match processes associated with a terminal (tty) listed in tidlist.
-n, --newest
Select only the newest (most recently started) of the matching processes.
-o, --oldest
Select only the oldest (least recently started) of the matching processes.
-x, --exact
Only match processes whose name or full command line exactly matches the pattern.
-v, --inverse
Invert the match; select processes that do not match the pattern.
-c, --count
Instead of printing PIDs, print the count of matching processes.
-d
Specify the delimiter used to separate printed PIDs (default is newline).
DESCRIPTION
The pgrep command is a utility used to look up processes based on criteria such as name, user, group, or parent process ID, and print the matching process IDs (PIDs). Unlike simply piping the output of ps to grep, pgrep is specifically designed for this task, making it more efficient and reliable for scripting purposes.
By default, pgrep matches against the process name (the contents of /proc/PID/stat), which is often just the executable name. To match against the full command line (including arguments), the -f option must be used. It supports regular expressions for pattern matching, offering powerful filtering capabilities.
pgrep returns a list of PIDs, one per line, which can be directly used by other commands like kill or renice. It's an essential tool for system administrators and scripters needing to automate process management tasks without complex parsing of ps output.
CAVEATS
By default, pgrep matches only the process name, not its arguments. This means searching for 'apache2' might work, but searching for 'apache2 -k start' will not unless the -f (full command line) option is used. Always consider using -f for more robust matching, especially in scripts.
Regular expressions can be powerful but also complex; ensure your pattern is specific enough to avoid unintended matches.
EXIT STATUS
The exit status of pgrep is 0 if one or more processes are found, 1 if no processes are found, and 2 for errors (e.g., invalid options or syntax). This makes it highly suitable for use in shell scripts for conditional execution.
PATTERN MATCHING
The pattern argument is interpreted as a POSIX extended regular expression (ERE). This allows for flexible and powerful matching, such as 'firefox|chrome' to find either browser, or 'ssh[d]?' to find 'ssh' or 'sshd'.
HISTORY
pgrep is part of the procps-ng suite of utilities, which provides process management tools for Linux. It was introduced to simplify tasks that previously required more complex shell pipelines involving ps and grep, improving both readability and efficiency for scripts. Its design specifically addresses the need to programmatically find process IDs, making it a modern and convenient alternative to older, more cumbersome methods.