pgrep介绍
1、pgrep是什么?
pgrep是一个命令行程序,可以根据输入给定的条件查找正在运行的程序的进程ID
它可以是完整或部分进程名称,运行该进程的用户或其他属性
语法:
1 | pgrep(选项)(参数) |
选项:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | -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 7237672378
-l 选项
# pgrep -f clickhouse-server -l72376 clckhouse-watch72378 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 -l201341 kdmflush201343 bioset201488 jbd2/dm-4-8201489 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/0root 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 sshd1511598205982860147