首頁·終端機命令

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