Pgrep


pgrep 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. 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 pidof and ps.

Example usage

The default behaviour of pgrep simplifies an otherwise complex task and is invoked with:

$ pgrep 'bash'

Which is roughly equivalent to:

$ ps ax | awk ' $5 ~ /bash/ '

Additional functionality of pgrep is listing the process name as well as the PID of all processes belonging to the group alice :

$ pgrep -l -G alice

showing all processes that do not belong to the user root by inverting the matching :

$ pgrep -v -u root

and only matching the most recently started process :

$ pgrep -n # The most recent process started
$ pgrep -n -u alice emacs # The most recent `emacs` process started by user `alice`