pgrep is used to look for processes whose command matches a certain string/pattern and/or optional conditions (i.e. owner, parent, process group, etc). As with most previous commands, let’s see how it works through examples:
root:~> pgrep dhclient → normal string match
30441
.
root:~> pgrep “[Dd]hclient?” → regex match
30441
.
root:~> pgrep “[Dd]hclient?” -c → count of regex matches
1
.
root:~> pgrep bash -d”,” → change default delimiter from “n” to “,”
2687,3971,10286,15342,25052,26114,30114
.
root:~> pgrep bash -n → shows only the newest process
26114
.
root:~> pgrep bash -o → shows only the oldest process
2687
.
root:~> pgrep “spawner” -f → matches not only the command but also all arguments
3179
.
root:~> pgrep “spawner” -f -l → same but shows the command
3179 gvfsdtrash
.
root:~> pgrep “spawner” -f -l -a → same but shows the whole command line
3179 /usr/libexec/gvfsdtrash spawner :1.5 /org/gtk/gvfs/exec_spaw/0
.
root:~> pgrep bash -U marc → matches only processes whose real UID is marc
.
root:~> pgrep bash -u marc → matches only processes whose effective UID is marc
.
root:~> pgrep bash -G dba → matches only processes whose real GID is dba
.
root:~> pgrep bash -g 1143 → matches only processes whose process group is 1143
.
root:~> pgrep bash -P 5540 → matches only processes whose parent ID is 5540
.
root:~> pgrep bash -t pts/3 → matches only process whose terminal is /dev/pts/3
________________________________________________________
root:~> pgrep bash -u marc -v → all processes owned by marc that DO NOT match the pattern
There are a three options more not shown in the examples above (–ns, -L and -s) but I’ll skip them as they are rarely used.