29

如何使用pgrep匹配

 5 years ago
source link: https://www.linuxprobe.com/pgrep-grep-ps.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

linux中查看进程信息,经常要用到pa auxf,具体到某个进程的时候,又需进行grep管道。如:ps auxf|grep java|grep -v grep ,即然这么长,可以不可以简化呢?当然可以,如果还是使用ps命令的话,可以改为ps auxf|grep jav[a],这样就不用排除自身了。不过感觉命令还是长了点,能不能再简点,可以,使用pgrep java ,不过此时得出的只是pid 。pgrep命令相当于如下的命令:

ps -eo pid,cmd | awk '{print $1,$2}' | grep KeyWord
参数说明:
选项 说明 -d 定义多个进程之间的分隔符, 如果不定义则使用换行符。 -P 根据父进程PID,找出所有字进程的pid -n 表示如果该程序有多个进程正在运行,则仅查找最新的,即最后启动的。 -o 表示如果该程序有多个进程正在运行,则仅查找最老的,即最先启动的(多个进程时即父进程PID)。 -G 其后跟着一组group id,该命令在搜索时,仅考虑group列表中的进程。 -u 其后跟着一组有效用户ID(effetive user id),该命令在搜索时,仅考虑该effective user列表中的进程。 -U 其后跟着一组实际用户ID(real user id),该命令在搜索时,仅考虑该real user列表中的进程。 -x 表示进程的名字必须完全匹配, 以上的选项均可以部分匹配。 -l 将不仅打印pid,也打印进程名。 -f 一般与-l合用, 将打印进程的参数。
使用示例
默认只显示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默认只能匹配进程的前15个字符串,个体可以参看ubuntu问答上的说明,如下:

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 -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

相关命令pkill ,用法基本和pgrep一致。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK