Where to start First steps track · 15 stages
Linux Labs

watch — periodically repeat a command

3 min procps

Repeats a command every N seconds on one screen; without quotes, a pipeline goes to the shell, not the command.

Cheat sheet

5 commands Printable version
  • watch -n1 -d 'command' every second, highlight changes
  • watch cat /proc/mdstat progress (RAID/jobs)
  • watch 'ss -tlnp | grep :80' quotes for the pipeline!
  • watch -g cmd exit when the output changes
  • watch -c 'docker ps' with color

Overview

watch repeats a command every N seconds and shows the fresh result on the same screen — handy for watching a process (a RAID rebuild, a queue, a file's size).

bash
watch command                     # every 2 seconds (the default)
watch -n1 command                 # every second
watch -d command                  # highlight what changed

Options

6

Always quote a command that has a pipeline or a wildcard (|, *) — otherwise the shell parses them before watch even runs, instead of the repeated command doing so. By default watch strips ANSI color from the output; -c turns it back on for commands that color their own text.

Option What it does Example
-n SECONDS Interval between runs in seconds; fractional values like 0.5 are allowed. watch -n1 'ss -tlnp | grep :80'
-d Highlight what changed compared to the previous output. watch -d free -h
-g Exit as soon as the command's output changes. watch -g 'systemctl is-active nginx'
-e Stop if the command exits with a non-zero status. watch -e 'curl -sf http://localhost/healthz'
-c Interpret ANSI color codes in the command's output. watch -c 'docker ps'
-t Hide the header showing the command name and time. watch -t 'df -h'

Nothing found. Try a shorter query.

Recipes

1

Examples

bash
watch -n1 'ss -tlnp | grep :80'   # watch listening ports (see ss)
watch -d free -h                  # memory changes (highlighted)
watch -d 'ps aux --sort=-%cpu | head'   # top by CPU, live
watch cat /proc/mdstat            # RAID rebuild progress (see mdadm)
watch -n5 'ls -lh /var/log/app.log'   # watch a log grow
watch -g 'systemctl is-active nginx'  # wait until the status changes
watch -c 'docker ps'              # with color, container states

Quote a command that has a pipeline/wildcard (watch 'ss | grep x'), otherwise | and * apply to watch itself, not the command.

Troubleshooting

4

A pipeline/grep "doesn't work" inside watch

Wrap the whole command in quotes: watch 'cmd | grep x'. Without quotes, the shell parses |/* before watch even sees them.

Colors disappeared

By default watch strips ANSI color. Turn it on: watch -c 'cmd' (if the command colors its own output).

Need to follow a log line by line

For logs, tail -f (see files-basics) or journalctl -f (see journalctl) are better; watch is for a "snapshot of state", not a stream.

Want to stop on a change / on readiness

watch -g cmd exits once the output changes (handy as "wait until ready"); watch -e cmd stops if the command errors out.

See also

  • system-monitoring — Load, CPU, memory, I/O — load average, top/htop, vmstat, iostat, free.
  • processes — Viewing, signaling, and prioritizing processes; finding CPU/memory hogs.
  • files-basics — Navigation and files — ls, cd, cp, mv, rm, cat, less, head, tail, copy-paste in the terminal.
Loading…