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 PATTERN

Parameters & Attributes

NameTypeDescription
-PoptionTakes a comma-separated parent PID list; restricts matches to their children.
-xoptionRequire the regular expression to match the entire process name.
PATTERNoperandExtended regular expression for the process name; -x does not make it a literal string.
PPIDoperandNumeric 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"
Linux Reference