# Pgrep

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Pgrep
> Markdown URL: https://mediated.wiki/source/Pgrep.md
> Source: https://en.wikipedia.org/wiki/Pgrep
> Source revision: 1353620233
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Command-line utility that searches and displays named processes}}
{{Lowercase title}}
{{Infobox software
| name                   = pgrep
| logo                   = 
| screenshot             = 
| screenshot size        = 
| caption                = 
| author                 = [Mike Shapiro](/source/Mike_Shapiro_(programmer))
| developer              = 
| released               = 
| latest release version = 
| latest release date    = 
| operating system       = [Unix](/source/Unix) and [Unix-like](/source/Unix-like)
| genre                  = [Command](/source/Command_(computing))
| license                = 
| website                = 
}}
<code>'''pgrep'''</code> is a [command-line](/source/command_line_interface) utility initially written for use with the [Solaris 7](/source/Solaris_(operating_system)) operating system by [Mike Shapiro](/source/Mike_Shapiro_(programmer)). It has since been available in [illumos](/source/illumos) and reimplemented for the [Linux](/source/Linux) and [BSD](/source/BSD)s ([DragonFly BSD](/source/DragonFly_BSD), [FreeBSD](/source/FreeBSD), [NetBSD](/source/NetBSD), and [OpenBSD](/source/OpenBSD)). It searches for all the named [processes](/source/process_(computing)) that can be specified as extended [regular expression](/source/regular_expression) patterns, and—by default—returns their [process ID](/source/Process_identifier). Alternatives include <code>[pidof](/source/pidof)</code> (finds process ID given a program name) and <code>[ps](/source/ps_(Unix))</code>.

==Example usage==
The default behaviour of <code>pgrep</code> (returning the [process identifier](/source/process_identifier) of the named tasks) simplifies an otherwise complex task and is invoked with:
<syntaxhighlight lang="bash">
$ pgrep 'bash'
</syntaxhighlight>

Which is roughly equivalent to:
<syntaxhighlight lang="bash">
$ ps ax | awk '{sub(/.*\//, "", $5)} $5 ~ /bash/ {print $1}'
</syntaxhighlight>

Additional functionality of <code>pgrep</code> is listing the process name as well as the PID (<kbd>-l</kbd> Lists the process name as well as the process ID) of all processes belonging to the group <code>alice</code> (<kbd>-G</kbd> Only match processes whose real group ID is listed. Either the numerical or symbolical value may be used):
<syntaxhighlight lang="bash">
$ pgrep -l -G alice
</syntaxhighlight>

showing all processes that do not belong to the user <code>[root](/source/Superuser)</code> (<kbd>-u euid</kbd> Only match processes whose effective user ID is listed. Either the numerical or symbolical value may be used) by inverting the matching (<kbd>-v</kbd> Negates the matching):
<syntaxhighlight lang="bash">
$ pgrep -v -u root
</syntaxhighlight>

and only matching the most recently started process (<kbd>-n</kbd> Select only the newest (most recently started) of the matching processes):
<syntaxhighlight lang="bash">
$ pgrep -n                # The most recent process started
$ pgrep -n -u alice emacs # The most recent `emacs` process started by user `alice`
</syntaxhighlight>

==See also==
{{Portal|Free and open-source software}}
* [List of Unix commands](/source/List_of_Unix_commands)
* <code>[pidof](/source/pidof)</code> — find the [process ID](/source/process_ID) of running programs 
* <code>[pkill](/source/pkill)</code> — signal processes based on name and other attributes
* <code>[ps](/source/ps_(Unix))</code> — display the currently running processes
* <code>[grep](/source/grep)</code> — search for lines of text that match one or many regular expressions

==References==
* {{man|1|pgrep|Solaris}}
* {{man|1|pgrep|die.net|look up processes based on name and other attributes}}

{{Unix commands}}

Category:Unix process- and task-management-related software

---
Adapted from the Wikipedia article [Pgrep](https://en.wikipedia.org/wiki/Pgrep) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Pgrep?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
