首页·终端命令

pgrep

进程与系统

用法

pgrep [-f] <pattern>

用和 pkill 完全相同的规则,只打印匹配到的 PID,所以它正好是动手杀之前的试运行。

常用选项

选项含义
-fMatch the full command line, so arguments count too.
-aPrint the command line next to the PID (Linux).
-lPrint the process name next to the PID.
-xExact name match only.
-u <user>Restrict to one user.
-cPrint how many matched instead of the PIDs.
-n / -oNewest / oldest match only.

示例

pgrep -af node

Every node process with its full command line.

pgrep -x sshd

The PID of sshd itself, not of anything merely mentioning ssh.

kill $(pgrep -f "celery worker")

Stop the processes the pattern found.

看什么在跑、什么在吃机器。先弄清是什么,再决定杀不杀。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. pgrep 是做什么的?

用和 pkill 完全相同的规则,只打印匹配到的 PID,所以它正好是动手杀之前的试运行。

Q. 怎么写?

pgrep [-f] <pattern> —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 7 个,完整列表在 man pgrep。这条命令属于进程与系统。

相关命令

man 手册: man pgrep