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.
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.serviceExit 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
