Linux command line

Linux watch command: rerun a command every few seconds

watch reruns a command and redraws the first screen of its output, every 2 seconds by default. watch -n 5 df -h sets a 5-second interval (the minimum is 0.1), -d highlights what changed and -g exits as soon as the output changes. Quote anything with a pipe, as in watch 'ps aux | grep python', so the whole pipeline reruns on every update.

5 min readLogs and monitoring

At a glance

  • watch refreshes every 2 seconds by default; -n SECONDS takes values from 0.1 to 2678400 (31 days).
  • -d highlights changes since the last update, and --differences=permanent keeps every change highlighted.
  • Wrap pipes, redirects and variables in single quotes so watch runs them through sh -c each time.
  • -g exits when the output changes, and -e stops when the command fails.

Run a command every N seconds

Running watch df -h clears your terminal, runs df -h every 2 seconds and fills the screen with the result. The top line shows a header such as Every 2.0s: df -h, followed by the hostname and the current time.

To change the wait between runs, pass -n (or --interval) with a number of seconds, or set the WATCH_INTERVAL environment variable for a new default. Fractions such as 0.5 work, anything below 0.1 is raised to 0.1, and anything above 2678400 is capped there.

Bash
watch df -h
watch -n 5 'ls -l /data'
watch -n 0.5 date

These check disk space at the default 2-second rate, list /data every 5 seconds and print the time twice a second. Press Ctrl+C to quit and return to your prompt.

The options worth knowing

These are the flags you will use most when monitoring jobs and system state:

OptionWhat it doesExample
-n, --intervalSeconds between runs, 2 by defaultwatch -n 5 df -h
-d, --differencesHighlights what changed since the last runwatch -d free -h
-t, --no-titleHides the header linewatch -t date
-g, --chgexitExits when the output changeswatch -g ls /data/done
-e, --errexitStops when the command fails and exits after a key presswatch -e ./check.sh
-c, --colorInterprets ANSI colorswatch -c ls --color=always
-p, --preciseTimes each run from the start of the previous onewatch -p -n 1 date
-x, --execRuns the command with exec instead of sh -cwatch -x ls -l

procps-ng 4.0.4 on Debian 13 also has -q to exit when the output stops changing for a number of cycles, -r to skip reruns when the window is resized and -w to turn off line wrapping. Run watch --help to see what your version supports.

Quote pipes and shell syntax

watch passes your command string to sh -c. If you write a pipe, redirect or variable without quotes, your own shell handles it before watch starts, so it pipes the output of watch itself into the next command instead of rerunning the pipeline.

Wrap any command that uses |, > or $VAR in quotes. Single quotes keep your shell from expanding variables, so sh -c expands them fresh on every run, while double quotes expand them once before watch starts. If you don't need shell features, -x hands the command straight to exec, which saves you the extra quoting.

Bash
watch 'ps aux | grep [p]ython'
watch -n 1 'grep "^cpu MHz" /proc/cpuinfo | sort -nrk4'

Both keep the pipe inside single quotes so it runs on every update. The second example comes from the watch man page and sorts your CPU cores by clock speed once per second.

Everyday watch commands

When a training job, data pipeline or batch script is running, a few short commands cover most of what you check in a second terminal pane:

  • nvidia-smi for GPU memory and utilization (see the nvidia-smi guide).
  • free -h with -d, so memory and swap numbers light up as they move.
  • df -h on a disk that a pipeline fills with large intermediate files.
  • ls -lt | head -5 on an output directory to see the newest checkpoints arrive.
  • kubectl get pods, though kubectl get also has its own -w (--watch) flag that streams changes.
  • squeue --me on a Slurm cluster, with a long interval such as -n 60, because the Slurm docs ask you not to run squeue in loops.
Bash
watch -n 1 nvidia-smi
watch -d free -h
watch -n 2 'ls -lt /data/out | head -5'
watch -n 60 squeue --me

By default watch waits -n seconds after the previous run finishes. Pass -p to measure the interval from the start of the previous run instead.

Exit when the output changes

You don't have to stare at watch until something happens. -g (or --chgexit) makes watch exit with code 0 as soon as the output changes, so you can chain a follow-up command with &&.

Bash
watch -g -n 5 'ls /data/done' && echo "new file arrived"

This checks /data/done every 5 seconds and runs echo once a file appears or disappears. When the command you watch might fail, -e freezes the screen on a non-zero exit with command exit with a non-zero status, press a key to exit, and -b rings the terminal bell instead.

Where watch falls short

watch only draws the first screen of output, keeps no history of earlier runs, needs an open terminal and can only alert you inside that terminal, with a beep or by exiting. To check a long Python script or batch command from your phone, a live link beats a terminal polling loop.

FAQ

How do I make watch refresh faster than once per second?

Pass a decimal number to -n, such as watch -n 0.5 date for two updates per second or watch -n 0.1 date for ten. Anything smaller than 0.1 is raised to 0.1.

How do I keep colors in watch output?

Pass -c (or --color) so watch interprets ANSI color sequences. Many tools turn their own colors off when they don't write to a terminal, so also pass their force flag, such as ls --color=always or grep --color=always.

Why does watch say command not found for my alias?

watch runs your command with sh -c, a non-interactive shell that doesn't load the aliases or functions from your .bashrc or .zshrc. Write out the full command, or put it in an executable script on your PATH.

How do I install watch on macOS?

macOS doesn't ship with watch. Install it with brew install watch, which gives you procps-ng watch 4.0.7 with the same flags as on Linux.

How do I save watch output to a file?

Redirecting watch itself captures screen-drawing codes, not clean text. Put tee -a inside the quoted command, as in watch 'date | tee -a log.txt', or use a shell while loop with sleep instead.

Check on long runs from your phone

Change one import and every tqdm bar prints a link you can open on any device. Get a Discord or Slack message when the run finishes, fails or stalls.

Get a live linkpip install tqdm-live

Sources