Pgrep
Encyclopedia
pgrep is a command-line utility initially written for use with the Solaris 7
Solaris Operating System
Solaris is a Unix operating system originally developed by Sun Microsystems. It superseded their earlier SunOS in 1993. Oracle Solaris, as it is now known, has been owned by Oracle Corporation since Oracle's acquisition of Sun in January 2010....

 operating system. It has since been reimplemented for Linux
Linux
Linux is a Unix-like computer operating system assembled under the model of free and open source software development and distribution. The defining component of any Linux system is the Linux kernel, an operating system kernel first released October 5, 1991 by Linus Torvalds...

 and the BSDs (Dragonfly BSD
DragonFly BSD
DragonFly BSD is a free Unix-like operating system created as a fork of FreeBSD 4.8. Matthew Dillon, an Amiga developer in the late 1980s and early 1990s and a FreeBSD developer between 1994 and 2003, began work on DragonFly BSD in June 2003 and announced it on the FreeBSD mailing lists on July...

, FreeBSD
FreeBSD
FreeBSD is a free Unix-like operating system descended from AT&T UNIX via BSD UNIX. Although for legal reasons FreeBSD cannot be called “UNIX”, as the direct descendant of BSD UNIX , FreeBSD’s internals and system APIs are UNIX-compliant...

, NetBSD
NetBSD
NetBSD is a freely available open source version of the Berkeley Software Distribution Unix operating system. It was the second open source BSD descendant to be formally released, after 386BSD, and continues to be actively developed. The NetBSD project is primarily focused on high quality design,...

, and OpenBSD
OpenBSD
OpenBSD is a Unix-like computer operating system descended from Berkeley Software Distribution , a Unix derivative developed at the University of California, Berkeley. It was forked from NetBSD by project leader Theo de Raadt in late 1995...

). It searches for all the named processes
Process (computing)
In computing, a process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system , a process may be made up of multiple threads of execution that execute instructions concurrently.A computer program is a...

 that can be specified as extended regular expression
Regular expression
In computing, a regular expression provides a concise and flexible means for "matching" strings of text, such as particular characters, words, or patterns of characters. Abbreviations for "regular expression" include "regex" and "regexp"...

 patterns, and—by default—returns their process ID
PID
-Medicine:*Prolapsed intervertebral disc, commonly called a "herniated disc"*Primary immune deficiency, disorders in which part of the body's immune system is missing or does not function properly...

. Alternatives include pidof
Pidof
pidof is a Linux utility that returns the process identifier of a running process or processes. On other operating systems, pgrep and ps are often used instead....

(finds process ID given a program name) and ps
Ps (Unix)
In most Unix-like operating systems, the ps program displays the currently-running processes. A related Unix utility named top provides a real-time view of the running processes....

.

Example usage

The default behaviour of pgrep (returning the process identifier
Process identifier
In computing, the process identifier is a number used by most operating system kernels to uniquely identify a process...

 of the named tasks) simplifies an otherwise complex task and is invoked with:


pgrep 'bash'


Which is roughly equivalent to:


ps ax | awk '/[p]rocessname/ {print $1}'
  1. With a redundant `grep`:
  2. ps ax | grep 'processname' | grep 'grep' -v | awk '{print $1}'



Additional functionality of pgrep 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 group alice (-G Only match processes whose real group ID is listed. Either the numerical or symbolical value may be used):


pgrep -l -G alice


showing all processes that do not belong to the user root
Superuser
On many computer operating systems, the superuser is a special user account used for system administration. Depending on the operating system, the actual name of this account might be: root, administrator or supervisor....

(-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 -u root


and only matching the most recently started process (-n Select only the newest (most recently started) of the matching processes):

  1. The most recent process started

pgrep -n
  1. The most recent `emacs` process started by user `alice`

pgrep -n -u alice emacs

See also

  • List of Unix programs
  • pidof
    Pidof
    pidof is a Linux utility that returns the process identifier of a running process or processes. On other operating systems, pgrep and ps are often used instead....

    — find the process ID of running programs
  • pkill
    Pkill
    pkill is a command-line utility initially written for use with the Solaris 7 operating system. It has since been reimplemented for Linux and some BSDs.As with the kill and killall commands, pkill is used to send signals to processes...

    – signal processes based on name and other attributes
  • ps
    Ps (Unix)
    In most Unix-like operating systems, the ps program displays the currently-running processes. A related Unix utility named top provides a real-time view of the running processes....

    – display the currently running processes
  • grep
    Grep
    grep is a command-line text-search utility originally written for Unix. The name comes from the ed command g/re/p...

    – search for lines of text that match one or many regular expressions
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK