Tutorials

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.

Cyber Defence

Recent Posts

How to Remove Files and Directories in Linux: rm, shred, rmdir

The command line does not have a Trash folder. When you delete a file from…

38 minutes ago

How to Create a systemd Service File in Linux: Step-by-Step Guide

Systemd is the init system on most modern Linux distributions. A service file (also called…

43 minutes ago

How to Create Users in Linux with useradd: A Complete Guide

Linux is a multi-user system. Each person or service that needs access should have its…

48 minutes ago

xargs Command in Linux: Build and Execute Commands from Input

The xargs command in Linux reads items from standard input and passes them as arguments to another…

2 days ago

locate Command in Linux: Find Files Fast with a Database Search

The locate command in Linux searches for files and directories by name. It queries a pre-built database…

2 days ago

Listing Linux Services with systemctl: A Complete Guide

Most modern Linux distributions use systemd as the default service manager. Knowing how to list…

2 days ago