Jump to content

pgrep

From RaySoft

The pgrep command searches the process table on the running system and prints the process IDs of all processes that match the criteria given on the command line.[1]

Documentation

  • man 1 'pgrep'

Syntax

pgrep [PARAMETER ...] PATTERN

Parameters

General
The following parameters can be used with all version of pgrep:
NOTE:
The BSD version only supports the short form (e.g. -f) of these parameters!
-f, --full
Match against full argument lists. The default is to match against process names.
-i, --ignore-case
Ignore case distinctions in both the process table and the supplied pattern.
-l, --list-name
Long output. pgrep prints the process name in addition to the process ID for each matching process. If used in conjunction with -f, prints the process ID and the full argument list for each matching process.
-P <PPID>, --parent <PPID>
Restrict matches to processes with a parent process ID in the comma-separated list PPID.
-U <UID>, --uid <UID>
Restrict matches to processes with a real user ID in the comma-separated list UID.
-x, --exact
Require an exact match of the process name, or argument list if -f is given. The default is to match any substring.
BSD
The following parameters can be used with the BSD version of pgrep:
-a
Include process ancestors in the match list. By default, the current pgrep process and all of its ancestors are excluded.
-q
Do not write anything to standard output.
GNU
The following parameters can be used with the GNU version of pgrep:
-A, --ignore-ancestors
Ignore all ancestors of pgrep. For example, this can be useful when elevating with sudo or similar tools.

Examples

Start applications in macOS if they exist and are not already started
vi "${HOME}/dev/lib/os.sh"
os::start_apps() {
  for app in "${@}"; do
    if ! open -a "${app}.app" -R >'/dev/null' 2>&1; then
      nf::carp "Error finding application: ${app}!"

      continue
    fi

    app="/Applications/${app}.app"

    if ! pgrep -f -q "${app}"; then
      open -a "${app}"
    fi
  done
}
os::start_apps 'Bitwarden'

References

  1. Andrew Doran. "PKILL(1)." Apple. https://www.raysoft.ch/data/man/pgrep.1.bsd.txt (accessed 19.08.2025)