watch
Overview
The watch command executes a program repeatedly and displays its output, allowing you to monitor changes over time. It’s essential for real-time system monitoring and observing command output changes.
Syntax
watch [options] commandCommon Options
| Option | Description |
|---|---|
-n seconds |
Update interval (default: 2) |
-d |
Highlight differences |
-t |
Turn off header |
-b |
Beep on command failure |
-e |
Exit on command failure |
-g |
Exit when output changes |
-c |
Interpret ANSI color sequences |
-x |
Pass command to exec instead of sh |
-p |
Precise timing |
Key Use Cases
- Monitor system resources
- Watch file changes
- Track process status
- Monitor network connections
- Observe command output changes
Examples with Explanations
Example 1: Monitor Disk Usage
watch df -hUpdates disk usage display every 2 seconds
Example 2: Watch Process List
watch -n 1 'ps aux | head -10'Updates process list every second
Example 3: Monitor with Differences
watch -d free -hHighlights changes in memory usage
Example 4: Watch File Size
watch -n 0.5 'ls -lh largefile.txt'Monitors file size changes every 0.5 seconds
System Monitoring
CPU usage:
watch -n 1 'cat /proc/loadavg'Memory usage:
watch -d 'free -h && echo && ps aux --sort=-%mem | head -5'Network connections:
watch -n 2 'netstat -tuln | grep LISTEN'
File and Directory Monitoring
Directory contents:
watch -d 'ls -la /tmp'File modifications:
watch -n 1 'stat file.txt | grep Modify'Log file growth:
watch -d 'wc -l /var/log/syslog'
Process Monitoring
Specific process:
watch -n 1 'ps aux | grep [p]rocess_name'Process tree:
watch -n 2 'pstree -p'Process resource usage:
watch -n 1 'top -bn1 | head -15'
Advanced Usage
Exit on change:
watch -g 'ls /tmp | wc -l'Beep on failure:
watch -b 'ping -c 1 google.com'Precise timing:
watch -p -n 0.1 'date +%S.%N'
Performance Analysis
- Low CPU overhead
- Configurable update intervals
- Good for long-term monitoring
- Minimal memory usage
- Efficient for repetitive tasks
Best Practices
- Use appropriate update intervals
- Combine multiple commands with &&
- Use quotes for complex commands
- Consider system load impact
- Use -d to highlight changes
Network Monitoring
Active connections:
watch -n 1 'ss -tuln'Network traffic:
watch -d 'cat /proc/net/dev'Ping monitoring:
watch -n 1 'ping -c 1 8.8.8.8 | tail -2'
Service Monitoring
Service status:
watch -n 5 'systemctl status apache2'Port availability:
watch -n 2 'nc -zv localhost 80'Database connections:
watch -n 3 'mysql -e "SHOW PROCESSLIST"'
Scripting Applications
Automated monitoring:
#!/bin/bash # Monitor until condition met watch -g 'test -f /tmp/done.flag' && echo "Process completed"Resource threshold monitoring:
watch -n 1 'free | awk "NR==2{printf \"%.2f%%\", \$3/\$2*100}"'
Integration Examples
With logging:
watch -n 10 'df -h | tee -a disk_usage.log'Combined monitoring:
watch -n 1 'echo "=== CPU ===" && uptime && echo "=== Memory ===" && free -h'Alert integration:
watch -n 30 'df -h | awk "$5 > 90 {print}" | mail -s "Disk Alert" admin'
Color and Formatting
Preserve colors:
watch -c 'ls --color=always'Custom formatting:
watch -n 1 'printf "\033[2J\033[H"; date; echo; ps aux | head -10'
Error Handling
Exit on command failure:
watch -e 'ping -c 1 unreachable_host'Beep on errors:
watch -b 'test -f important_file.txt'Continue on errors:
watch 'command_that_might_fail || echo "Command failed"'
Troubleshooting
- High CPU usage with short intervals
- Terminal size limitations
- Command quoting issues
- Color display problems
- Timing precision limitations
Security Considerations
- Avoid displaying sensitive information
- Be careful with command injection
- Monitor resource usage
- Consider screen locking
- Validate command inputs
Alternative Approaches
Using while loop:
while true; do clear command sleep 2 doneUsing inotify for file watching:
inotifywait -m /path/to/file
Real-world Examples
Development monitoring:
watch -n 1 'make test 2>&1 | tail -10'Deployment monitoring:
watch -d 'kubectl get pods'Performance testing:
watch -n 0.5 'curl -w "%{time_total}\n" -o /dev/null -s http://localhost'
Customization
Custom header:
watch -t 'echo "Custom Monitor - $(date)"; echo; command'Multiple commands:
watch 'echo "=== Disk ===" && df -h && echo "=== Memory ===" && free -h'