首頁·終端機命令

pkill

行程與系統

用法

pkill [-SIGNAL] [-f] <pattern>

把符合某個模式的行程全部殺掉;不加 `-f` 只比對行程名稱,而模式寫得太寬會連帶殺掉別的,所以先用同樣的模式跑一遍 pgrep。

常用選項

選項含義
-fMatch against the whole command line, not just the process name.
-u <user>Restrict to one user.
-xRequire the whole name to match, not a substring.
-n / -oOnly the newest / only the oldest matching process.
-9Force instead of asking the process to exit.
-iCase-insensitive matching.

範例

pgrep -af "node server.js" && pkill -f "node server.js"

Look first, then kill exactly what you saw.

pkill -u deploy -f gunicorn

Stop the deploy user's gunicorn workers.

看什麼在跑、什麼在吃機器。先弄清是什麼,再決定殺不殺。

怎麼看

  • 方括號 [ ] 表示這部分可以不寫。
  • 省略號 … 表示可以寫多個。
  • 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。

常見問題

Q. pkill 是做什麼的?

把符合某個模式的行程全部殺掉;不加 `-f` 只比對行程名稱,而模式寫得太寬會連帶殺掉別的,所以先用同樣的模式跑一遍 pgrep。

Q. 怎麼寫?

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

Q. 值得記的選項有幾個?

這裡列了 6 個,完整列表在 man pkill。這條命令屬於行程與系統。

相關命令

man 手冊: man pkill