commands
pgrep — find matching processes
Print matching PIDs. -P restricts parents and -x requires exact process-name match. Exit 1 means no match; a match is not authorization to signal. Use a fresh private workspace, ordinary user and bounded child processes; no sudo.
Formal Syntax & Structure
linux syntaxlinux
pgrep -P PPID -x PATTERNParameters & Attributes
| Name | Type | Description |
|---|---|---|
| -P | option | Takes a comma-separated parent PID list; restricts matches to their children. |
| -x | option | Require the regular expression to match the entire process name. |
| PATTERN | operand | Extended regular expression for the process name; -x does not make it a literal string. |
| PPID | operand | Numeric parent process ID or comma-separated list. |
Return Value
exit status and output
Print matching PIDs. -P restricts parents and -x requires exact process-name match. Exit 1 means no match; a match is not authorization to signal.
Working Example
Example Snippetlinux
sleep 2 &child=$!rc=0pids=$(pgrep -P "$$" -x sleep) || rc=$?wait "$child" || exit 1test "$rc" -eq 0 || exit 1printf "%s\n" "$pids" | grep -Fx "$child" >/dev/null || exit 1printf "child found\n"