Linux常用命令-pgrep

命令

pgrep

描述

look up or signal processes based on name and other attributes
根据名称查找进程PID

用法

1
pgrep [options] pattern

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
pattern支持正则

Options:
-d, --delimiter <string> 指定输出分隔符
-l, --list-name 显示PID和进程名称
-a, --list-full 显示PID和完整的命令行
-v, --inverse 取反
-w, --lightweight 显示所有的TID
-c, --count 显示匹配到的进程数量
-f, --full 使用完整的进程名进行匹配
-g, --pgroup <PGID,...> 指定进程组PGID
-G, --group <GID,...> 指定GID
-n, --newest 选择最近启动的进程
-o, --oldest 选择最近最少启动的进程
-P, --parent <PPID,...> 指定父进程PPID查看子进程
-s, --session <SID,...> 指定会话SID
-t, --terminal <tty,...> 指定终端
-u, --euid <ID,...> 指定用户名
-U, --uid <ID,...> 指定UID
-x, --exact 精确匹配命令行
-F, --pidfile <file> 从文件读取PID
-L, --logpidfile 如果PID文件没有被锁定则失败
--ns <PID> 匹配与属于同一命名空间的进程
--nslist <ns,...> 指定命名空间,可选ipc, mnt, net, pid, user, uts

注意

pgrep命令是根据条件过滤进程信息

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
$ pgrep sshd
928
6832
# -d指定输出的分隔符
$ pgrep -d, ssh
928,6832
# -l显示PID和进程名称
$ pgrep -l sshd
928 sshd
6832 sshd
# -a显示PID和完整命令
$ pgrep -a ssh
928 /usr/sbin/sshd -D
6832 sshd: root@pts/0,pts/1
# -c统计过滤结果
$ pgrep -c ssh
2
# ps查看进程启动的时间
$ ps aux | grep ssh
root 928 0.0 0.2 112936 4296 ? Ss 13:14 0:00 /usr/sbin/sshd -D
root 6832 0.0 0.2 157512 5944 ? Ss 16:35 0:01 sshd: root@pts/0,pts/1
# -n显示最近启动的进程
$ pgrep -n sshd
6832
# -o显示最早启动的进程
$ pgrep -o sshd
928
$ pgrep -an sshd
6832 sshd: root@pts/0,pts/1
$ pgrep -ao sshd
928 /usr/sbin/sshd -D
# 通过ps查看ssh相关的父进程PPID,可以看到sshd有两个子进程
$ ps -ef |grep ssh
root 928 1 0 13:15 ? 00:00:00 /usr/sbin/sshd -D
root 6832 928 0 16:36 ? 00:00:01 sshd: root@pts/0
root 9045 928 0 18:45 ? 00:00:00 sshd: root@pts/1
# -P指定父进程的PPID查询子进程信息
$ pgrep -aP 928
6832 sshd: root@pts/0
9045 sshd: root@pts/1
# ps查看mysql相关进程,可以看到mysqld_safe是root用户,mysqld是mysql用户
$ ps aux | grep mysql
root 953 0.0 0.0 115540 1700 ? S 13:15 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/mysql.pid
mysql 2100 0.1 2.7 809236 55092 ? Sl 13:15 0:29 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql ...
$ pgrep -l mysql
953 mysqld_safe
2100 mysqld
# -u指定用户,只显示mysql用户的进程
$ pgrep -lu mysql mysql
2100 mysqld
# id命令获取mysql用户的uid
$ id mysql
uid=997(mysql)
# -U指定UID
$ pgrep -lU 997
2100 mysqld
# -v取反,即不显示root和mysql用户的进程
$ pgrep -lvu root,mysql
647 polkitd
657 dbus-daemon
8379 chronyd
# ps查看启动tty和pts终端信息
$ ps aux | grep tty
root 944 0.0 0.0 110204 852 tty1 Ss+ 13:14 0:00 /sbin/agetty --noclear tty1 linux
$ ps aux | grep pts
root 6832 0.0 0.2 157512 5944 ? Ss 16:35 0:01 sshd: root@pts/0,pts/1
root 6861 0.0 0.1 116060 2628 pts/0 Ss+ 16:35 0:00 -bash
root 6889 0.0 0.1 116188 2916 pts/1 Ss 16:35 0:00 -bash
root 8819 0.0 0.0 155448 1860 pts/1 R+ 18:40 0:00 ps aux
# -t显示指定终端的进程PID
$ pgrep -at tty1
944 /sbin/agetty --noclear tty1 linux
$ tty
/dev/pts/1
$ pgrep -at pts/1
6889 -bash
# 结束对应的终端窗口
$ kill -9 6889
# -x精确匹配命令名称
$ pgrep -x ssh
$ pgrep -ax sshd
928 /usr/sbin/sshd -D
6832 sshd: root@pts/0
9045 sshd: root@pts/1
# -F读取文件中的PID,注意仅支持指定单个PID
$ echo 8379 > file
$ pgrep -aF file
8379 /usr/sbin/chronyd
# 指定多个PID无效,只读取第一个值
$ pgrep ssh > file
$ more file
928
6832
9045
$ pgrep -aF file
928 /usr/sbin/sshd -D
# 配合ps命令过滤并查看具体进程信息
$ ps -fp $(pgrep -d, ssh)
UID PID PPID C STIME TTY TIME CMD
root 928 1 0 13:15 ? 00:00:00 /usr/sbin/sshd -D
root 6832 928 0 16:36 ? 00:00:01 sshd: root@pts/0
root 9045 928 0 18:45 ? 00:00:00 sshd: root@pts/1