pgrep 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

pgrep 命令安装:


-bash: pgrep: command not found

#Debian
apt-get install procps

#Ubuntu
apt-get install procps

#Alpine
apk add procps

#Arch Linux
pacman -S procps-ng
 
#Kali Linux
apt-get install procps

#CentOS
yum install procps-ng

#Fedora
dnf install procps-ng

#Raspbian
apt-get install procps

#Docker
docker run cmd.cat/pgrep pgrep

pgrep 命令补充说明:


pgrep 命令以名称为依据从运行进程队列中查找进程,并显示查找到的进程 ID。每一个进程 ID 以一个十进制数表示,通过一个分割字符串和下一个 ID 分开,默认的分割字符串是一个新行。对于每个属性选项,用户可以在命令行上指定一个以逗号分割的可能值的集合。

pgrep 命令语法:


pgrep [options] pattern

pgrep 命令选项:


-a, --list-full:列出PID和完整命令行;
-o, --oldest:仅显示找到的最小(起始)进程号;
-n, --newest:仅显示找到的最大(结束)进程号;
-P, --parent :指定父进程号;
-g, --pgroup :指定进程组;
-t, --terminal :指定开启进程的终端;
-u, --euid :指定进程的有效用户ID。
-c, --count (仅限pgrep)抑制正常输出; 而是打印匹配过程的计数。 当count与任何内容都不匹配时,例如,返回零,该命令将返回非零值。
-d, --delimiter delimiter (仅限pgrep)设置用于在输出中分隔每个进程ID的字符串(默认情况下为换行符)。
-l, --list-name (仅限pgrep)列出进程名称以及进程ID。

pgrep 命令参数:


进程名称:指定要查找的进程名称,同时也支持类似grep指令中的匹配模式。

pgrep 命令实例


[root@TestMaster ~]
# pgrep -lo nginx
28870 nginx

[root@TestMaster ~]
# pgrep -l nginx
14890 nginx
14891 nginx
14892 nginx
14893 nginx
28870 nginx
#默认只显示PID
root@361way:~# pgrep zabbix
2380
2381
2382
2383
2384
2385

#-l 同时显示PID和ProcessName
root@361way:~# pgrep -l zabbix
2380 zabbix_agentd
2381 zabbix_agentd
2382 zabbix_agentd
2383 zabbix_agentd
2384 zabbix_agentd
2385 zabbix_agentd

#-o 当匹配多个进程时,显示进程号最小的那个
root@361way:~# pgrep -l -o zabbix
2380 zabbix_agentd

#-n 当匹配多个进程时,显示进程号最大的那个
root@361way:~# pgrep -l -n zabbix
2385 zabbix_agentd

使用 pgrep -f 可以进行进程全字符匹配,示例如下:

#使用ps命令可以正常grep到进程
root@361way:~# ps auxf|grep druid
root 25713 0.0 0.0 8108 940 pts/0 S+ 06:08 0:00 _ grep --color=auto druid
dev 7438 1.3 11.5 5524888 884988 ? Sl Jun16 672:54 java -server -Xmx4g -XX:MaxNewSize=1g -XX:+UseCompressedOops -XX:+UseParNewGC -Duser.timezone=UTC -Dfile.encoding=UTF-8 -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UseCMSInitiatingOccupancyOnly -XX:+PrintHeapAtGC -XX:+PrintGCApplicationConcurrentTime -XX:+PrintGCApplicationStoppedTime -Djava.io.tmpdir=/data/tmpdata/java.io.tmpdata -Xloggc:/data/tmpdata/java.io.tmpdata/coordinator-gc.log -classpath lib/*:config/coordinator io.druid.cli.Main server coordinator

#pgrep的匹配结果为空
root@361way:~# pgrep druid

#加上-f参数后,正常得到进程pid
root@361way:~# pgrep -f druid
7438

root@361way:~# pgrep -f -l druid
7438 java -server -Xmx4g -XX:MaxNewSize=1g -XX:+UseCompressedOops -XX:+UseParNewGC -Duser.timezone=UTC -Dfile.encoding=UTF-8 -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UseCMSInitiatingOccupancyOnly -XX:+PrintHeapAtGC -XX:+PrintGCApplicationConcurrentTime -XX:+PrintGCApplicationStoppedTime -Djava.io.tmpdir=/data/tmpdata/java.io.tmpdata -Xloggc:/data/tmpdata/java.io.tmpdata/coordinator-gc.log -classpath lib/*:config/coordinator io.druid.cli.Main server coordinator

使用 -P 参数可以输出指定父进程的子进程,如:

root@361way:~# pgrep -P 2380
2381
2382
2383
2384
2385

pgrep 命令的坑:


这里需要特别指出的是 pgrep 默认只能匹配进程的前 15 个字符串

ps aux includes the full command line (path and parameters),
while pgrep only looks at the first 15 characters of the executable's names

pgrep 命令扩展阅读:




pgrep 命令评论

共收录到 489Linux 命令