watch

Execute a command periodically and display its output fullscreen.

Category: System monitoring terminal automation debugging

Install

# Usually part of procps-ng

# RHEL / Alma / Rocky
sudo dnf install procps-ng

# Debian / Ubuntu
sudo apt install procps

# openSUSE
sudo zypper install procps

# Arch
sudo pacman -S procps-ng

What it does

watch runs a command repeatedly at a fixed interval and displays the output in a fullscreen terminal view. It is commonly used to monitor changing system state in real time.

How it works (mechanical)

  • Executes the specified command at regular intervals (default: 2 seconds).
  • Clears and redraws the screen each cycle.
  • Shows timestamp and command header by default.
  • Can highlight differences between successive outputs.
  • Runs until interrupted (Ctrl+C).

Quick Start

# Monitor disk usage every 2 seconds
watch df -h

10 Practical Examples

# 1) Monitor running processes
watch ps aux
# 2) Check memory usage
watch free -h
# 3) Run every 1 second
watch -n 1 date
# 4) Highlight differences
watch -d netstat -tulnp
# 5) Monitor a specific log file
watch tail -n 20 /var/log/messages
# 6) Suppress header line
watch -t uptime
# 7) Monitor file changes
watch ls -l /tmp
# 8) Watch system load
watch -n 2 cat /proc/loadavg
# 9) Combine with grep for filtering
watch "ps aux | grep nginx"
# 10) Exit with Ctrl+C when finished

Notes & Gotchas

  • Default interval is 2 seconds unless changed with -n.
  • Quoting is required for complex commands with pipes.
  • Screen is cleared each cycle.
  • Output width depends on terminal size.
  • For interactive monitoring, tools like top or htop may be better.

Historical Context

watch became popular as a lightweight real-time monitoring tool before advanced interactive dashboards were common.

Modern Equivalent

While modern monitoring systems exist (Prometheus, Grafana, etc.), watch remains invaluable for quick CLI diagnostics and troubleshooting on servers.

Related Commands

  • top — interactive process viewer.
  • htop — enhanced process viewer.
  • tail — view end of files.
  • ps — display process status.
  • uptime — show system uptime and load.