pgrep介绍

1、pgrep是什么?
pgrep是一个命令行程序,可以根据输入给定的条件查找正在运行的程序的进程ID
它可以是完整或部分进程名称,运行该进程的用户或其他属性
 
语法:
pgrep(选项)(参数)

 

选项:

-d, --delimiter <string>  specify output delimiter
 -l, --list-name           list PID and process name
 -a, --list-full           list PID and full command line
 -v, --inverse             negates the matching
 -w, --lightweight         list all TID
 -c, --count               count of matching processes
 -f, --full                use full process name to match
 -g, --pgroup <PGID,...>   match listed process group IDs
 -G, --group <GID,...>     match real group IDs
 -n, --newest              select most recently started
 -o, --oldest              select least recently started
 -P, --parent <PPID,...>   match only child processes of the given parent
 -s, --session <SID,...>   match session IDs
 -t, --terminal <tty,...>  match by controlling terminal
 -u, --euid <ID,...>       match by effective IDs
 -U, --uid <ID,...>        match by real IDs
 -x, --exact               match exactly with the command name
 -F, --pidfile <file>      read PIDs from file
 -L, --logpidfile          fail if PID file is not locked
 --ns <PID>                match the processes that belong to the same

   

参数:
进程名称:指定要查找的进程名称,同时也支持类似grep指令中的匹配模式
 
例子:
-f 选项
# pgrep -f clickhouse-server 
72376
72378

 

-l 选项
# pgrep -f clickhouse-server -l
72376 clckhouse-watch
72378 clickhouse-serv

 

-d 选项
# pgrep -f clickhouse-server -l -d ' '
72376 clckhouse-watch 72378 clickhouse-serv

 

-u 选项
# 组合模式,查找root或者j-huangxianghai-jk下的进程
pgrep -u root,j-huangxianghai-jk -l
201341 kdmflush
201343 bioset
201488 jbd2/dm-4-8
201489 ext4-rsv-conver

# 打印在用户j-huangxianghai-jk用户下且名称中包含sshd
pgrep -u j-huangxianghai-jk sshd -l

 

特别说明:
1)pgrep相当于# ps -eo pid,cmd | awk '{print $1,$2}' | grep clickhouse-server
72378 /usr/bin/clickhouse-server

 

2)pgrep查找的是程序名,不包括其参数
[root@ck21 j-huangxianghai-jk]# ps aux|grep sshd
root       1511  0.0  0.0 106056  4176 ? Ss   Oct18   0:00 /usr/sbin/sshd -D
root      59820  0.0  0.0 145760  5300 ? Ss   13:59   0:00 sshd: j-huangxianghai-jk [priv]
j-huang+  59828  0.0  0.0 147844  3048 ? S    13:59   0:00 sshd: j-huangxianghai-jk@pts/0
root      60147  0.0  0.0 145760  5296 ? Ss   14:00   0:00 sshd: j-huangxianghai-jk [priv]
[root@ck21 j-huangxianghai-jk]# pgrep j-huangxianghai-jk
[root@ck21 j-huangxianghai-jk]# pgrep sshd
1511
59820
59828
60147

 


posted on 2021-10-23 17:30  gentleman_hai  阅读(2669)  评论(0编辑  收藏  举报

导航