ps
进程与系统
用法
ps aux | ps -ef把此刻正在运行的进程拍成一张快照;`ps aux` 是不带减号的 BSD 写法,`ps -ef` 是 POSIX 写法,把两种混着用是最常见的错误。
常用选项
| 选项 | 含义 |
|---|---|
| aux | BSD form (no dash): every process of every user, including those with no terminal. Works on Linux and macOS. |
| -ef | POSIX form: the same list with the parent PID and the full command line. |
| -p <pid> | Show one process only. |
| -u <user> | Only the processes of that user. |
| -o pid,ppid,%cpu,%mem,comm | Choose exactly which columns to print. |
| --sort=-%cpu | Sort by CPU, highest first. GNU procps only; macOS ps has no --sort. |
示例
ps aux | grep -i nginxFind the nginx processes and their PIDs.
ps -eo pid,%mem,comm --sort=-%mem | headThe ten processes using the most memory (Linux).
ps -p 1234 -o etime=How long PID 1234 has been running, with no header.
看什么在跑、什么在吃机器。先弄清是什么,再决定杀不杀。
怎么看
- 方括号 [ ] 表示这部分可以不写。
- 省略号 … 表示可以写多个。
- 选项区分大小写——有些命令里 -r 和 -R 不是一回事。
常见问题
Q. ps 是做什么的?
把此刻正在运行的进程拍成一张快照;`ps aux` 是不带减号的 BSD 写法,`ps -ef` 是 POSIX 写法,把两种混着用是最常见的错误。
Q. 怎么写?
ps aux | ps -ef —— 方括号表示可以省略的部分。
Q. 值得记的选项有几个?
这里列了 6 个,完整列表在 man ps。这条命令属于进程与系统。
相关命令
man 手册: man ps