首頁·終端機命令

ps

行程與系統

用法

ps aux | ps -ef

把此刻正在執行的行程拍成一張快照;`ps aux` 是不帶減號的 BSD 寫法,`ps -ef` 是 POSIX 寫法,把兩種混著用是最常見的錯誤。

常用選項

選項含義
auxBSD form (no dash): every process of every user, including those with no terminal. Works on Linux and macOS.
-efPOSIX 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,commChoose exactly which columns to print.
--sort=-%cpuSort by CPU, highest first. GNU procps only; macOS ps has no --sort.

範例

ps aux | grep -i nginx

Find the nginx processes and their PIDs.

ps -eo pid,%mem,comm --sort=-%mem | head

The 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