Operations 6 min read

Master Real-Time System Monitoring with Linux’s watch Command

The article explains how to use Linux’s watch command to automatically repeat commands at set intervals, customize refresh rates, highlight changes, suppress headers, trigger beeps on errors, and exit on output changes, providing practical examples for monitoring processes, files, and user logins.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Real-Time System Monitoring with Linux’s watch Command

watch Command Syntax

The watch utility repeatedly runs a specified command at fixed intervals and updates the terminal with its output. By default it executes the command every two seconds until interrupted with Ctrl+C.

watch <options> <command>

Run a Command Every X Seconds

Use the -n option followed by the number of seconds to change the refresh interval. watch -n 5 <command> Example: list a directory every two seconds.

watch ls -l ~/

Highlight Changes Between Updates

The -d option highlights differences between the previous and current output, making it easy to spot changes.

watch -d <command>

Hide Header

By default, watch shows a header with the interval, command, and timestamp. Suppress it with -t.

watch -t <command>

Beep on Errors

The -b option triggers a beep when the monitored command exits with a non‑zero status. This requires the beep package to be installed.

watch -b sudo systemctl status sshd.service

Exit When Output Changes

Use the -g option to make watch terminate as soon as the command’s output changes.

Example: wait for a specific file to appear.

watch -g "ls -l | grep filename" && echo "file arrived"

Example: notify when a user logs in.

watch -g "who | grep username" && echo "username logged in"

Monitor System Activity

The watch command can be combined with other Linux utilities (e.g., top, df, netstat) to continuously observe system resources, file changes, or user activity, reducing the need to manually re‑run commands.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Automationcommand-linesystem-monitoringwatch
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.