{{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 | developer = | released = | latest release version = | latest release date = | operating system = Unix and Unix-like | genre = Command | license = | website = }} <code>'''pgrep'''</code> is a command-line utility initially written for use with the Solaris 7 operating system by Mike Shapiro. It has since been available in illumos and reimplemented for the Linux and BSDs (DragonFly BSD, FreeBSD, NetBSD, and OpenBSD). It searches for all the named processes that can be specified as extended regular expression patterns, and—by default—returns their process ID. Alternatives include <code>pidof</code> (finds process ID given a program name) and <code>ps</code>.
==Example usage== The default behaviour of <code>pgrep</code> (returning the 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</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 * <code>pidof</code> — find the process ID of running programs * <code>pkill</code> — signal processes based on name and other attributes * <code>ps</code> — display the currently running processes * <code>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