Cybersecurity Updates & Tools

watch Command in Linux: Monitor Changing Output in Real Time

The watch command in Linux runs a command at regular intervals and refreshes the terminal with the latest output. It is the simplest way to monitor anything that changes over time — disk usage, load averages, active connections — without writing a loop.

How to Use the watch Command in Linux

The syntax is:

bashwatch [OPTIONS] COMMAND

Run a command under watch:

bashwatch date

The terminal clears and a header bar appears at the top. The left side shows the interval and the command being run (Every 2.0s: date). The right side shows the current time. Without options, watch refreshes every 2 seconds.

To hide the header bar:

bashwatch -t uptime

Exit at any time with Ctrl+C.

If watch is not installed, install the procps package:

bashsudo apt install procps          # Ubuntu, Debiansudo dnf install procps-ng       # Fedora, RHEL

Highlight Changes, Control the Interval, and Handle Pipes

Change the refresh interval with -n. Values are in seconds, and fractional intervals are supported:

bashwatch -n 5 df -h        # refresh every 5 secondswatch -n 0.5 uptime     # refresh twice per second

Highlight differences between successive outputs with -d. Any value that differs from the previous run is highlighted:

bashwatch -d uptime

For sticky highlighting — all values that have ever changed stay marked — use -d=cumulative:

bashwatch -d=cumulative vmstat 1 1

Pipes require quotes. The shell parses | before passing arguments to watch. Without quotes, watch controls only the first command in the pipeline and the rest runs outside its refresh cycle:

bash# Wrong: only 'netstat -anp' runs inside watchwatch netstat -anp | grep ':80'# Correct: the full pipeline refreshes on each cyclewatch "netstat -anp | grep -c ':80\b.*LISTEN'"

Single or double quotes both work. The quoted string is treated as one argument and re-evaluated on every interval.

Exit Automatically and Run Complex Commands

Two flags let watch stop on a condition instead of waiting for Ctrl+C:

  • -g (–chgexit) — exits when the command output changes
  • -e (–errexit) — freezes the display and exits when the command returns a non-zero exit code

Exit as soon as an error string appears in a log:

bashwatch -g grep -c "ERROR" /var/log/syslog

Stop automatically if a service goes down:

bashwatch -e -n 5 systemctl is-active nginx

-e is useful in monitoring scripts that need to trigger a remediation action when a service fails.

Commands with shell features — variables, subshells, or multiple statements — must be wrapped in bash -c:

bashwatch -n 2 bash -c 'echo "Load:"; uptime | awk -F"load average:" "{print \$2}"'

watch does not inherit your shell’s aliases or functions. Wrap those in bash -c as well:

bashwatch bash -c 'your_alias'

Background monitoring. watch is an interactive terminal program and is not suited for background use. For monitoring without a live display, use a cron job or a shell loop:

bashwhile true; do command; sleep 5; done &

Use -n to control the refresh rate, -d to highlight what is changing, and always quote commands that contain pipes. Reach for -g or -e when you need watch to exit on a specific condition rather than running indefinitely. Leave a comment below if you run into any issues.