Skip to content
Latchkey

watch: Repeat a Command to Observe Change in CI

watch re-runs a command every few seconds and redraws its output, so you can observe a value change, like memory creeping up, without a loop.

During an interactive debug session on a runner, watch turns any one-shot command into a live monitor: tail the highest-memory process, the free count, or a port coming up.

What it does

watch clears the screen and re-runs the given command every N seconds (default 2), showing the latest output. -d highlights characters that changed since the last run, -n sets the interval, and -t removes the header. It is an interactive, full-screen tool.

Common usage

Terminal
watch -n 1 'ps aux --sort=-%mem | head'
watch -d 'free -m'               # highlight what changed
watch -n 2 'ss -ltnp | grep :8080'   # wait for a port to open
watch -n 5 'df -h /'             # disk filling during a build

Options

FlagWhat it does
-n <sec>Interval between runs (default 2)
-dHighlight differences between updates
-tHide the header line
-gExit when the output changes
-eExit (freeze) if the command returns non-zero

In CI

watch is interactive and full-screen, so it does not belong in an automated job; it never exits and produces no useful log. For unattended monitoring in a script, use a loop with sleep or the timeout command instead. Reserve watch for an interactive SSH session into a runner to catch a transient peak (memory just before an OOM, a port appearing).

Common errors in CI

Putting watch in a CI step hangs the job until it times out, because watch loops forever by design. Quote the command so the shell does not expand pipes prematurely: watch 'ps aux | head', not watch ps aux | head (the latter pipes watch’s own output). For "run until a condition, then stop", -g exits on first change.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →