Linux watch Command

Execute Command Periodically - 10 Practical Examples with Detailed Explanations

Example 1

Basic Command Monitoring

$ watch ls -l
Every 2.0s: ls -l total 12 -rw-r--r-- 1 user user 1024 Oct 18 14:25 file1.txt -rw-r--r-- 1 user user 2048 Oct 18 14:28 file2.txt
Executes a command repeatedly at regular intervals (default 2 seconds) and displays the output full-screen. Press Ctrl+C to exit. Perfect for monitoring files, processes, or any changing system state.
Default Interval: Updates every 2 seconds. Use -n to change the interval
Example 2

Custom Update Interval

$ watch -n 5 df -h
Every 5.0s: df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 100G 45G 50G 48% /
Changes the update interval using the '-n' option followed by the number of seconds. This example checks disk usage every 5 seconds. Useful when monitoring slower-changing metrics.
Interval Range: Can be as fast as 0.1 seconds or as slow as you need
Example 3

Highlight Differences Between Updates

$ watch -d free -h
Every 2.0s: free -h total used free Mem: 15Gi 8.2Gi 2.8Gi Swap: 2.0Gi 0B 2.0Gi
Highlights differences between consecutive updates using the '-d' option. Changed values are displayed in reverse video, making it easy to spot what's changing.
Visual Tracking: Highlights make it easy to see which values are actively changing
Example 4

Hide Header Information

$ watch -t date
Sat Oct 18 14:30:00 EDT 2025
Removes the header showing interval, command, hostname, and timestamp using the '-t' option. Displays only the command output without any watch metadata.
Clean Display: Shows only command output, no watch metadata
Example 5

Exit on Command Failure

$ watch -e 'ls /tmp/file 2>/dev/null'
# Exits when command returns non-zero status
Automatically exits watch when the monitored command returns a non-zero exit status using the '-e' option. Perfect for waiting until a condition occurs.
Automation: Wait for specific conditions in scripts
Example 6

Monitor Process Count

$ watch -n 1 'ps aux | grep nginx | wc -l'
Every 1.0s: ps aux | grep nginx | wc -l 5
Monitors the number of nginx processes every second. Commands with pipes must be quoted. Perfect for monitoring process counts and tracking service health.
Shell Commands: Always quote complex commands with pipes
Example 7

Monitor Log File

$ watch -n 2 'tail -10 /var/log/syslog'
Every 2.0s: tail -10 /var/log/syslog Oct 18 14:29:58 hostname systemd[1]: Started Session Oct 18 14:30:00 hostname CRON[12345]: CMD
Continuously displays the last 10 lines of a log file, updating every 2 seconds. Excellent for real-time log monitoring during troubleshooting or deployments.
Real-Time Logs: Monitor logs during debugging
Example 8

Monitor Network Connections

$ watch -n 1 'ss -s'
Every 1.0s: ss -s Total: 180 TCP: 12 (estab 3, closed 4)
Tracks network connection statistics every second. Useful for monitoring server load and detecting connection issues.
Modern Tool: ss is the modern replacement for netstat
Example 9

Custom Monitoring Dashboard

$ watch -n 2 'echo "=== CPU ===" && uptime && echo "" && echo "=== Memory ===" && free -h'
Every 2.0s: echo "=== CPU ===" && ... === CPU === 14:30:00 up 5 days, 3:30, 2 users === Memory === total used free Mem: 15Gi 8.2Gi 2.8Gi
Creates a custom dashboard by combining multiple commands. The entire command string must be quoted. Perfect for creating personalized monitoring views.
Custom Dashboards: Combine any commands for tailored monitoring
Example 10

Monitor with Color Output

$ watch -c 'ls -l --color=always'
# Preserves color codes for colored display
Preserves color output from commands using the '-c' option. The '--color=always' flag forces color, and watch's '-c' preserves it. Makes output more readable.
Colored Output: Combine watch -c with --color=always for better visibility