Operations 5 min read

Master Linux’s watch Command: Real‑Time Monitoring and Advanced Tips

This guide explains how to install and use the Linux watch command for periodic command execution, system‑load monitoring, live log viewing, custom script scheduling, and several advanced options such as custom intervals, output redirection, screen clearing, timestamps, and display formatting.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux’s watch Command: Real‑Time Monitoring and Advanced Tips

Installation and Basic Usage

Ensure watch is installed; most Linux distributions include it by default. If missing, install it with the package manager:

# Using apt (Debian/Ubuntu)
sudo apt-get install watch

# Using yum (CentOS/Fedora)
sudo yum install watch

The basic syntax is watch [options] command. By default the command runs every 2 seconds; the interval can be changed with the -n option.

Monitoring System Load

Run watch uptime to refresh the output of uptime every 2 seconds, showing the system’s average load. Stop the monitoring with Ctrl+C.

Monitoring Log Files

Combine watch with tail to watch a log file in real time, for example:

watch tail /var/log/syslog

This displays the latest lines of /var/log/syslog and updates the view whenever new entries appear.

Running Custom Scripts Periodically

Create a shell script, make it executable, then schedule it with watch at a chosen interval.

# myscript.sh
#!/bin/bash
echo "Current time: $(date)"
chmod +x myscript.sh
watch -n 5 ./myscript.sh

The script runs every 5 seconds, printing the current time on each refresh.

Advanced Usage Examples

1. Custom refresh interval

Use -n to set a custom interval, e.g. watch -n 10 command refreshes every 10 seconds.

2. Save output to a file

Redirect the output of watch for later analysis: watch command > output.txt.

3. Clear screen and show title

The -t option clears the terminal before each refresh, keeping the display clean: watch -t command.

4. Show timestamps

Add -p to prepend a timestamp to each refresh, for example watch -p -n 5 command.

5. Custom display format

Combine options to hide the title and timestamp while still using a custom interval, e.g. watch -D -t -p -n 5 command.

Conclusion

The watch utility is a powerful, multi‑purpose tool for periodic command execution, live log monitoring, and simple automation, enabling administrators to manage and observe Linux systems more efficiently.

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.

Linuxcommand-lineShell scriptingwatch command
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.