Original author(s) | Mike Shapiro |
---|---|
Operating system | Unix andUnix-like |
Type | Command |
pgrep
is acommand-line utility initially written for use with theSolaris 7 operating system byMike Shapiro. It has since been available inillumos and reimplemented for theLinux andBSDs (DragonFly BSD,FreeBSD,NetBSD, andOpenBSD). It searches for all the namedprocesses that can be specified as extendedregular expression patterns, and—by default—returns theirprocess ID. Alternatives includepidof
(finds process ID given a program name) andps
.
The default behaviour ofpgrep
(returning theprocess identifier of the named tasks) simplifies an otherwise complex task and is invoked with:
$pgrep'bash'
Which is roughly equivalent to:
$psax|awk'{sub(/.*\//, "", $5)} $5 ~ /bash/ {print $1}'
Additional functionality ofpgrep
is listing the process name as well as the PID (-l Lists the process name as well as the process ID) of all processes belonging to the groupalice
(-G Only match processes whose real group ID is listed. Either the numerical or symbolical value may be used):
$pgrep-l-Galice
showing all processes that do not belong to the userroot
(-u euid Only match processes whose effective user ID is listed. Either the numerical or symbolical value may be used) by inverting the matching (-v Negates the matching):
$pgrep-v-uroot
and only matching the most recently started process (-n Select only the newest (most recently started) of the matching processes):
$pgrep-n# The most recent process started$pgrep-n-ualiceemacs# The most recent `emacs` process started by user `alice`
pidof
— find theprocess ID of running programspkill
— signal processes based on name and other attributesps
— display the currently running processesgrep
— search for lines of text that match one or many regular expressions