pkill
进程与系统
用法
pkill [-SIGNAL] [-f] <pattern>把匹配某个模式的进程全部杀掉;不加 `-f` 只比对进程名,而模式写得太宽会连带杀掉别的,所以先用同样的模式跑一遍 pgrep。
常用选项
| 选项 | 含义 |
|---|---|
| -f | Match against the whole command line, not just the process name. |
| -u <user> | Restrict to one user. |
| -x | Require the whole name to match, not a substring. |
| -n / -o | Only the newest / only the oldest matching process. |
| -9 | Force instead of asking the process to exit. |
| -i | Case-insensitive matching. |
示例
pgrep -af "node server.js" && pkill -f "node server.js"Look first, then kill exactly what you saw.
pkill -u deploy -f gunicornStop the deploy user's gunicorn workers.
看什么在跑、什么在吃机器。先弄清是什么,再决定杀不杀。
怎么看
- 方括号 [ ] 表示这部分可以不写。
- 省略号 … 表示可以写多个。
- 选项区分大小写——有些命令里 -r 和 -R 不是一回事。
常见问题
Q. pkill 是做什么的?
把匹配某个模式的进程全部杀掉;不加 `-f` 只比对进程名,而模式写得太宽会连带杀掉别的,所以先用同样的模式跑一遍 pgrep。
Q. 怎么写?
pkill [-SIGNAL] [-f] <pattern> —— 方括号表示可以省略的部分。
Q. 值得记的选项有几个?
这里列了 6 个,完整列表在 man pkill。这条命令属于进程与系统。
相关命令
man 手册: man pkill