Docs / Linux Basics / How to Use watch to Monitor Command Output in Real Time

How to Use watch to Monitor Command Output in Real Time

By Admin · Mar 15, 2026 · Updated Apr 23, 2026 · 199 views · 2 min read

The watch command repeatedly runs a command and displays its output, making it easy to monitor changes in real time. It is one of the simplest yet most useful tools for server administration.

Basic Usage

# Run a command every 2 seconds (default)
watch free -h

# Run every 5 seconds
watch -n 5 df -h

# Highlight differences between updates
watch -d free -h

Practical Monitoring Examples

System Resources

# Watch memory usage
watch -n 2 free -h

# Watch disk space
watch -n 30 df -h

# Watch load average
watch -n 1 uptime

# Watch network connections count
watch -n 2 "ss -s"

Process Monitoring

# Watch a specific process
watch -n 1 "ps aux | grep nginx | grep -v grep"

# Watch process count
watch -n 5 "ps aux | wc -l"

# Watch MySQL connections
watch -n 2 "mysqladmin -u root processlist"

Log Monitoring

# Watch recent log entries
watch -n 5 "tail -5 /var/log/nginx/error.log"

Deployment Monitoring

# Watch HTTP response during deployment
watch -n 2 "curl -s -o /dev/null -w '%{http_code} %{time_total}s' http://localhost/"

# Watch container status
watch -n 2 "docker ps --format 'table {{.Names}}	{{.Status}}	{{.Ports}}'"

# Watch systemd service during restart
watch -n 1 "systemctl status nginx --no-pager | head -10"

Advanced watch Usage

# Exit when the output changes (useful in scripts)
watch -g "cat /var/run/service.pid"

# Use with complex commands (wrap in quotes)
watch -n 5 "netstat -an | grep :80 | wc -l"

# No header output
watch --no-title -n 5 "df -h /"

# Use precise interval
watch -n 1 -p uptime

Alternatives to watch

# For log files, use tail -f instead:
tail -f /var/log/syslog

# For more sophisticated monitoring:
# htop     — Interactive process viewer
# iotop    — I/O monitoring
# iftop    — Network traffic monitoring
# nload    — Network bandwidth monitoring
# glances  — All-in-one monitoring dashboard

Tips

  • Press q to quit watch
  • Use -d to highlight changes — makes it easy to spot differences
  • Wrap complex commands in quotes to prevent shell interpretation issues
  • Use -n 1 sparingly — running commands every second can add load
  • Combine with grep and awk to filter output to only what matters

Was this article helpful?