首页·终端命令

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