TrumanWong

pgrep

Find and list qualified process IDs (PIDs) in the currently running process based on the information given by the user

Supplementary instructions

pgrep command searches the process from the running process queue based on its name and displays the found process ID. Each process ID is represented as a decimal number, separated from the next ID by a delimiter string. The default delimiter string is a new line. For each attribute option, the user can specify a comma-separated set of possible values on the command line.

grammar

pgrep(options)(parameters)

Options

-d, --delimiter <string>: Specify the output delimiter.
-l, --list-name: List process ID and process name.
-a, --list-full: List process ID and complete command line.
-v, --inverse: reverse matching, that is, only unmatched processes are displayed.
-w, --lightweight: List all TIDs (lightweight threads).
-c, --count: Display the number of matching processes.
-f, --full: Use the full process name for matching.
-g, --pgroup <PGID,...>: Match the specified process group ID.
-G, --group <GID,...>: Match the real group ID.
-i, --ignore-case: Ignore case for matching.
-n, --newest: Select the most recently started process.
-o, --oldest: Select the earliest started process.
-O, --older <seconds>: Select processes that started earlier than the specified number of seconds.
-P, --parent <PPID,...>: Match only child processes of the given parent process.
-s, --session <SID,...>: Match session ID.
-t, --terminal <tty,...>: Match according to the controlling terminal.
-u, --euid <ID,...>: Match based on valid user ID.
-U, --uid <ID,...>: Match based on real user ID.
-x, --exact: Exactly match the process name.
-F, --pidfile <file>: Read PID from file.
-L, --logpidfile: Fail if the PID file is not locked.
-r, --runstates <state>: Match running states (D, S, Z, etc.).
--ns <PID>: Match processes in the same namespace as the specified PID.
--nslist <ns,...>: List the namespaces that will be considered in the --ns option. Available namespaces include: ipc, mnt, net, pid, user, uts.

Parameters

Process name: Specify the process name to be found, and also supports matching patterns similar to the grep command.

Example

pgrep -lo httpd
4557 httpd
  [root@localhost ~]# pgrep -ln httpd
4566 httpd

[root@localhost ~]# pgrep -l httpd
4557 httpd
4560 httpd
4561 httpd
4562 httpd
4563 httpd
4564 httpd
4565 httpd
4566 httpd

[root@localhost ~]# pgrep httpd 4557
4560
4561
4562
4563
4564
4565
4566

[root@localhost ~]# pgrep -x httpd
4557
4560
4561
4562
4563
4564
4565
4566