← 返回命令列表

Linux command

pgrep 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Find process by name

pgrep [nginx]

Find with full command line match

pgrep -f "[python script.py]"

List process names and PIDs

pgrep -l [ssh]

Find processes by user

pgrep -u [username] [process]

Find newest matching process

pgrep -n [chrome]

Find oldest matching process

pgrep -o [firefox]

Count matching processes

pgrep -c [apache]

Exact name match

pgrep -x [bash]

说明

pgrep searches for processes by name or attributes, returning their PIDs. It's a more focused alternative to piping ps through grep. Pattern matching is against the process name by default (executable name). The -f flag extends matching to the full command line including arguments. Regular expressions are supported. Selection filters narrow results by user, group, terminal, or parent process. These can be combined for precise targeting. The -v flag inverts selection, finding processes that don't match. Newest (-n) and oldest (-o) options return single matches, useful when multiple instances exist. Count mode (-c) reports how many processes match without listing them. Output formatting options control delimiter and detail level. These enable integration with shell scripts and other tools.

参数

-l, --list-name
List PID and process name.
-a, --list-full
List PID and full command line.
-f, --full
Match against full command line.
-x, --exact
Match exactly (not substring).
-n, --newest
Select newest (most recent) match.
-o, --oldest
Select oldest (first) match.
-c, --count
Count matches instead of listing.
-d _DELIM_, --delimiter _DELIM_
Output delimiter (default: newline).
-u _USER_, --euid _USER_
Match effective user ID.
-U _USER_, --uid _USER_
Match real user ID.
-g _GROUP_, --pgroup _GROUP_
Match process group.
-G _GROUP_, --group _GROUP_
Match real group ID.
-P _PPID_, --parent _PPID_
Match parent process ID.
-t _TERM_, --terminal _TERM_
Match controlling terminal.
-v, --inverse
Invert match (negation).
-i, --ignore-case
Case-insensitive matching.

FAQ

What is the pgrep command used for?

pgrep searches for processes by name or attributes, returning their PIDs. It's a more focused alternative to piping ps through grep. Pattern matching is against the process name by default (executable name). The -f flag extends matching to the full command line including arguments. Regular expressions are supported. Selection filters narrow results by user, group, terminal, or parent process. These can be combined for precise targeting. The -v flag inverts selection, finding processes that don't match. Newest (-n) and oldest (-o) options return single matches, useful when multiple instances exist. Count mode (-c) reports how many processes match without listing them. Output formatting options control delimiter and detail level. These enable integration with shell scripts and other tools.

How do I run a basic pgrep example?

Run `pgrep [nginx]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -l, --list-name do in pgrep?

List PID and process name.