Continuous Command Monitoring in Linux with watch
When monitoring background file transfers, waiting for storage volumes to synchronize, tracking real-time directory growth, or watching process memory utilisation in Linux, repeatedly pressing the up-arrow key and hitting Enter to re-run a command is inefficient. Instead of crafting clunky custom while true; do ... sleep; done shell loops, Linux system administrators use the standard watch command. Part of the core procps-ng package, watch executes any specified terminal command at fixed intervals, clears and redraws the display in full screen, and visually highlights altered text characters as values evolve.
Basic watch Syntax and Default Behaviour
The standard syntax for the watch utility is straightforward:
watch [options] command
By default, if you run watch without specifying any arguments, it executes the target command every 2.0 seconds. For example, to track free memory allocations in real time:
watch free -h
The utility clears your terminal window and establishes a clean top banner displaying the refresh interval, the exact command being executed, and the current system timestamp. To terminate the monitoring session at any point, press Ctrl + C.
Customising the Execution Interval with -n
Depending on the resource being audited, a 2-second interval may either be too rapid (causing unnecessary CPU load) or too sluggish. You can specify custom intervals using the -n (interval in seconds) flag:
watch -n 5 df -h
The -n parameter supports sub-second precision, allowing you to sample fast-moving queues with high-resolution polling down to tenths of a second:
watch -n 0.5 tail -n 20 /var/log/syslog
Highlighting Dynamic Changes with -d
One of the most powerful features of watch is its ability to highlight textual differences between successive iterations using the -d (differences) switch:
watch -d -n 1 ls -lh /tmp/incoming_uploads
Whenever a file size increases, a new file arrives, or a timestamp changes, watch renders the altered text in inverted colour fill. This allows you to spot subtle modifications in long tables or numerical matrices at a glance.
If you want the highlight to persist across multiple cycles rather than only marking differences from the immediate prior run, use cumulative highlighting:
watch -d=cumulative uptime
Monitoring Complex Commands, Aliases, and Pipes
Because watch executes target commands by default using sh -c, passing commands containing pipes, subshells, or multiple commands requires careful quoting. If quotes are omitted, your current shell will interpret the pipe before handing the command to watch.
To watch a pipeline accurately, encapsulate the entire expression in single or double quotes:
watch -n 2 'ps aux | grep nginx | wc -l'
Similarly, if you need to evaluate environment variables or shell redirection:
watch -n 3 'cat /proc/loadavg && echo "--- Top CPU ---" && ps -eo pid,pcpu,comm --sort=-pcpu | head -n 6'
Exiting Automatically on Output Changes with -g
Modern versions of watch include the -g (chgexit) option, which instructs the command to terminate immediately as soon as the output of the monitored command changes. This is invaluable in shell scripts for synchronisation hurdles:
watch -g -n 2 'ping -c 1 192.168.1.1 >/dev/null 2>&1; echo $?' && notify-send "Host is reachable!"
The loop will poll the target IP silently until the exit code toggles, at which point watch halts and your subsequent shell chained commands execute automatically.
Hiding the Header Banner for Clean Terminal Displays
When projecting terminal status boards on wall monitors or embedding outputs into multi-pane tmux sessions, the top header bar can waste vertical screen estate. Append the -t (no-title) flag to suppress the top banner entirely:
watch -t -n 1 nvidia-smi
This renders a clean, edge-to-edge diagnostic display focused entirely on raw operational data.