watch Cheatsheet
Quick reference for rerunning commands at intervals and monitoring output changes with watch in Linux
The `watch` command reruns another command at a fixed interval and refreshes the output in the terminal. This cheatsheet covers intervals, highlighting changes, command pipelines, and practical monitoring patterns.
Basic Syntax
Core watch command forms.
| Command | Description |
|---|---|
watch command | Run a command repeatedly every 2 seconds |
watch -n 5 command | Refresh every 5 seconds |
watch -n 1 date | Update output every second |
watch --help | Show available options |
Common Monitoring Tasks
Use watch to keep an eye on changing system state.
| Command | Description |
|---|---|
watch free -h | Monitor memory usage |
watch df -h | Monitor disk space |
watch uptime | Check load averages and uptime |
| `watch “ps -ef | grep nginx”` |
watch "ss -tulpn" | Monitor listening sockets |
Timing and Refresh Control
Control how often watch reruns the command.
| Command | Description |
|---|---|
watch -n 0.5 command | Refresh every 0.5 seconds |
watch -n 10 command | Refresh every 10 seconds |
watch -t command | Hide the header line |
watch -p command | Try to run at precise intervals |
watch -x command arg1 arg2 | Run the command directly without sh -c |
Highlighting Changes
Make changing output easier to spot.
| Command | Description |
|---|---|
watch -d command | Highlight differences between updates |
watch -d free -h | Highlight memory changes |
watch -d "ip -brief address" | Highlight interface state or address changes |
watch -d -n 1 "cat /proc/loadavg" | Highlight load-average changes |
Commands with Pipes and Quotes
Wrap pipelines and shell syntax in quotes unless you use -x.
| Command | Description |
|---|---|
| `watch “ps -ef | grep apache”` |
| `watch “ls -lh /var/log | tail”` |
watch "grep -c error /var/log/syslog" | Count matching lines repeatedly |
watch -x ls -lh /var/log | Run ls directly without shell parsing |
| `watch “find /tmp -maxdepth 1 -type f | wc -l”` |
Exit Conditions and Beeps
Stop or alert when output changes.
| Command | Description |
|---|---|
watch -g command | Exit when command output changes |
watch -b command | Beep if the command exits with non-zero status |
watch -b -n 5 "systemctl is-active nginx" | Alert if the service is no longer active |
watch -g "cat /tmp/status.txt" | Exit when the file content changes |
Troubleshooting
Quick fixes for common watch issues.
| Issue | Check |
|---|---|
| Pipes or redirects do not work | Quote the whole command or use sh -c |
| The screen flickers too much | Increase the interval or hide the header with -t |
| Output changes are hard to spot | Add -d to highlight differences |
| The command exits immediately | Check the command syntax outside watch first |
| You need shell expansion and variables | Use quoted shell commands instead of -x |
Related Guides
Use these guides for broader monitoring and process tasks.
| Guide | Description |
|---|---|
| Linux Watch Command | Full guide to watch options and examples |
| ps Command in Linux | Inspect running processes |
| free Command in Linux | Check memory usage |
| ss Command in Linux | Inspect sockets and ports |
| top Command in Linux | Monitor processes interactively |