首頁·終端機命令

watch

行程與系統

用法

watch -n <seconds> "<command>"

每隔幾秒重新執行同一條命令並重畫畫面;帶管線或 `*` 的命令一定要加引號,否則 shell 只會先對 watch 本身展開一次,而且 macOS 預設沒有它。

常用選項

選項含義
-n 5Interval in seconds; the default is 2.
-dHighlight what changed since the last run.
-tDrop the header line.
-gExit as soon as the output changes.
-xRun the command directly instead of through a shell.

範例

watch -n 5 "kubectl get pods"

Quote it, or the shell hands watch only the first word.

watch -d free -h

Watch memory move, with the changes highlighted.

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

怎麼看

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

常見問題

Q. watch 是做什麼的?

每隔幾秒重新執行同一條命令並重畫畫面;帶管線或 `*` 的命令一定要加引號,否則 shell 只會先對 watch 本身展開一次,而且 macOS 預設沒有它。

Q. 怎麼寫?

watch -n <seconds> "<command>" —— 方括號表示可以省略的部分。

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

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

相關命令

man 手冊: man watch