watch
Repeatedly run a command and show periodic full-screen updates
By CMD Script Team · 4 min read · Last updated
watch [OPTIONS] COMMANDOptions
| Flag | Description |
|---|---|
-n | Set the interval in seconds between runs, e.g. -n 2 (default is 2 seconds) |
-d | Highlight the differences between successive updates |
-g | Exit as soon as the command's output changes from the previous run |
-t | Turn off the title/header bar showing the command and timestamp |
-b | Beep if the command exits with a non-zero status |
-x | Pass the command to exec directly instead of through sh -c, preserving exit codes/signals more precisely |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS (not preinstalled; install via Homebrew: brew install watch)
What it does
watch runs a given command repeatedly (every 2 seconds by default) and redraws the
terminal with the latest output each time, turning any one-shot command into a live,
full-screen dashboard. It's a lightweight alternative to writing a while true; do ...; sleep N; done loop, and adds conveniences like highlighting what changed between runs
and a header showing the command and last-refresh time.
Beginner examples
watch df -h— refresh disk usage every 2 secondswatch -n 5 free -h— refresh memory usage every 5 secondswatch uptime— keep an eye on load average, updating continuously- Press
Ctrl+Cto stop watching and return to the prompt
watch -n 5 free -h
Advanced examples
- Highlight exactly what changed between refreshes:
watch -d df -h - Watch a piped/composed command (must be quoted):
watch 'ps aux | grep nginx' - Stop automatically the moment output changes, e.g. waiting for a job to finish:
watch -g 'ps aux | grep myjob' - Watch without the header bar, for cleaner output to screenshot or pipe elsewhere:
watch -t 'date +%T' - Beep audibly if the watched command starts failing:
watch -b 'curl -sf https://example.com/health'
watch -d -n 1 'ss -tn state established'
Common mistakes
- Forgetting to quote commands containing pipes, redirects, or multiple arguments —
watch ps aux | grep nginxrunswatch ps auxand pipes that intogrep, not what most people intend; the correct form iswatch 'ps aux | grep nginx'. - Using
watchfor something that needs true real-time updates faster than its interval allows — very short intervals (sub-second) can add noticeable overhead from repeatedly forking the command. - Watching a command that has side effects (like sending a request or writing a file) and not realizing it's actually running that command over and over, not just re-displaying a static result.
- Not realizing
watchclears the screen each cycle — scrollback of previous outputs is lost unless you log the underlying command's output separately.
Tips
- Use
-dliberally when watching something that changes slowly — it turns "did anything change?" from an eyeball exercise into an instantly visible highlight. -g(exit on change) is great for waiting on a condition, like a process disappearing or a file appearing, without polling manually in a loop.- Pair
watchwith-n 1for near-real-time views of fast-changing state like active network connections (watch -n 1 'ss -tn'). -t(no title) is handy when you want to capture just the command's raw output, e.g. for a screen recording or simplified display.
Best practices
- Always quote multi-part commands passed to
watchto make sure the whole pipeline runs underwatch, not just the first command. - Avoid setting extremely short intervals (sub-second) for expensive commands — the overhead of repeatedly spawning the command can itself affect system load.
- Prefer purpose-built monitoring tools (
top,htop,iostat -x 1) over wrapping them inwatch, since they already have built-in refresh loops and are more efficient. - Use
watch -ginstead of manually polling in a custom loop when you just need to block until a condition changes — it's simpler and less error-prone.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Watching disk space fill up during a large file transfer or log rotation:
watch -d df -h. - Monitoring the number of established connections to a service while debugging a
connection leak:
watch -n 1 'ss -tn state established | wc -l'. - Waiting for a background deployment or batch job to finish by watching for its process
to disappear:
watch -g 'pgrep -f my_batch_job'.
Common interview questions
- Why do you need to quote a piped command when using watch?
watchtreats its argument as the command to run; without quotes, the shell interprets the pipe immediately and only passes the first command towatch, breaking the intended pipeline. - How would you make a script wait until a process disappears, without polling
manually?
watch -g 'pgrep -f process_name'— the-gflag exits watch the moment output changes, which happens oncepgrepstops finding the process. - What's the difference between watch and just writing a while loop with sleep?
watchprovides the loop, a refresh header, and built-in diff highlighting (-d) for free; a manualwhile true; do ...; sleep N; doneloop requires you to reimplement clearing the screen and formatting yourself.
Frequently Asked Questions
How do I change how often watch refreshes?
Use -n followed by the number of seconds, e.g. watch -n 5 df -h refreshes every 5 seconds instead of the 2-second default.
How do I make watch highlight what changed between refreshes?
Use the -d flag: watch -d df -h highlights any characters that differ from the previous update, making it easy to spot what changed at a glance.
Why do I need quotes around commands with pipes or redirects when using watch?
watch passes its argument to the shell as a single command, so a pipeline like watch 'ps aux | grep nginx' needs quotes; without them, the shell running your interactive session would try to interpret the pipe itself before watch ever sees it.
Does watch work on macOS by default?
No, watch isn't preinstalled on macOS. Install it via Homebrew with brew install watch, or use a shell loop (while true; do clear; df -h; sleep 2; done) as a fallback.
Cheat sheet
Download a quick-reference cheat sheet for watch.