pgrep
进程与系统
用法
pgrep [-f] <pattern>用和 pkill 完全相同的规则,只打印匹配到的 PID,所以它正好是动手杀之前的试运行。
常用选项
| 选项 | 含义 |
|---|---|
| -f | Match the full command line, so arguments count too. |
| -a | Print the command line next to the PID (Linux). |
| -l | Print the process name next to the PID. |
| -x | Exact name match only. |
| -u <user> | Restrict to one user. |
| -c | Print how many matched instead of the PIDs. |
| -n / -o | Newest / oldest match only. |
示例
pgrep -af nodeEvery node process with its full command line.
pgrep -x sshdThe 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