watch

Execute a program periodically and display output in real-time

Syntax: watch [options] command
Note: watch runs the specified command repeatedly at regular intervals and displays the output, allowing you to monitor changes in real-time.

Description

The watch command executes a program periodically and displays its output in full screen. This is useful for monitoring system changes, tracking file modifications, or observing command output over time. By default, watch runs the command every 2 seconds.

Command Options

Option Description
-n, --interval seconds Specify update interval in seconds
-d, --differences Highlight differences between updates
-g, --chgexit Exit when command output changes
-t, --no-title Turn off header showing interval and command
-b, --beep Beep if command has non-zero exit status
-e, --errexit Exit if command has non-zero exit status
-c, --color Interpret ANSI color sequences
-x, --exec Pass command to exec instead of sh -c

Basic Usage

Simple monitoring:
# Monitor current date and time
watch date

# Monitor disk usage
watch df -h

# Monitor memory usage
watch free -h

# Monitor network connections
watch netstat -tuln

Custom Intervals

Setting update intervals:
# Update every 5 seconds
watch -n 5 date

# Update every 0.5 seconds (500ms)
watch -n 0.5 'ps aux | head -10'

# Update every 10 seconds
watch -n 10 'df -h | grep sda'

Highlighting Changes

Difference highlighting:
# Highlight changes between updates
watch -d 'ps aux | head -10'

# Highlight changes with custom interval
watch -d -n 1 'cat /proc/loadavg'

# Exit when output changes
watch -g 'ls -la /tmp'

System Monitoring Examples

Process monitoring:
# Monitor top processes
watch -n 2 'ps aux --sort=-%cpu | head -10'

# Monitor specific process
watch -n 1 'ps aux | grep apache'

# Monitor process count
watch 'ps aux | wc -l'
File system monitoring:
# Monitor directory contents
watch -d 'ls -la /var/log'

# Monitor file size changes
watch -n 1 'ls -lh /var/log/syslog'

# Monitor disk space
watch -d 'df -h /'
Network monitoring:
# Monitor network interfaces
watch -n 1 'cat /proc/net/dev'

# Monitor active connections
watch -d 'netstat -tuln | grep LISTEN'

# Monitor network statistics
watch 'ss -s'

Advanced Usage

Complex commands:
# Monitor multiple metrics
watch -n 2 'echo "=== Load Average ===" && cat /proc/loadavg && echo "=== Memory ===" && free -h'

# Monitor log file tail
watch -n 1 'tail -10 /var/log/syslog'

# Monitor command with pipes
watch -n 5 'ps aux | grep -v grep | grep apache | wc -l'
Error handling:
# Beep on command failure
watch -b 'ping -c 1 google.com'

# Exit on command failure
watch -e 'test -f /tmp/important_file'

# Exit when output changes
watch -g 'cat /proc/sys/kernel/random/entropy_avail'

Color and Formatting

Color support:
# Enable color interpretation
watch -c 'ls --color=always'

# Remove header
watch -t 'date'

# Combine options
watch -c -d -n 1 'ls --color=always -la /tmp'

Practical Monitoring Scenarios

Web server monitoring:
# Monitor Apache processes
watch -d 'ps aux | grep apache2 | grep -v grep'

# Monitor web server connections
watch -n 2 'netstat -tuln | grep :80'

# Monitor access log
watch -n 1 'tail -5 /var/log/apache2/access.log'
Database monitoring:
# Monitor MySQL processes
watch -d 'mysqladmin processlist'

# Monitor database connections
watch -n 5 'netstat -tuln | grep :3306'

# Monitor database size
watch -n 10 'du -sh /var/lib/mysql'
System performance:
# Monitor CPU usage
watch -n 1 'cat /proc/loadavg'

# Monitor memory usage
watch -d -n 2 'cat /proc/meminfo | head -10'

# Monitor I/O statistics
watch -n 2 'iostat -x 1 1'

Keyboard Controls

Key Action
Ctrl+C Exit watch
q Quit watch
Space Force immediate update
h Show help

Tips and Best Practices

Optimization tips:
# Use appropriate intervals to avoid system load
watch -n 5 'heavy_command'  # Instead of default 2 seconds

# Quote complex commands properly
watch 'command1 | command2'

# Use exec for better performance with simple commands
watch -x ls /tmp

# Combine with other tools
watch -d 'tail -n 20 /var/log/syslog | grep ERROR'

Common Use Cases

  • System monitoring: Track CPU, memory, and disk usage
  • Process monitoring: Watch specific processes or services
  • File monitoring: Observe file changes and directory contents
  • Network monitoring: Track network connections and statistics
  • Log monitoring: Watch log files for new entries
  • Service monitoring: Monitor service status and health
  • Development: Watch build processes or test results
  • Troubleshooting: Monitor system behavior during issues

Alternatives and Related Commands

Similar functionality:
# Using while loop instead of watch
while true; do clear; date; sleep 2; done

# Using tail for log monitoring
tail -f /var/log/syslog

# Using top for process monitoring
top -d 2

# Using htop for interactive process monitoring
htop

Troubleshooting

Common issues:
# If command contains special characters, quote it
watch 'ps aux | grep "my process"'

# For commands with pipes, use quotes
watch 'command1 | command2 | command3'

# If watch doesn't update, check if command is hanging
watch -n 1 'timeout 5 your_command'

# To see command errors
watch -e 'your_command 2>&1'
Related Commands: top, htop, tail, ps, netstat