watch — monitor repeated output
💬 "watch command"
watch periodically re-executes a command and shows changes. It is convenient for observing progress.
The default interval is 2 seconds, and you can adjust it with -n.
- Concept: Periodically refreshes and displays command output.
- -n: Specifies the interval in seconds.
- -d: Highlights changed parts.
- Top: Shows the running command and the current time.
Commonly Used Options
| Option | Description |
|---|---|
| -n <seconds> | Specifies the refresh interval |
| -d | Highlights differences |
| -t | Hides the top header |
| -g | Exits when output changes (for scripts) |
Three Essential Command Combinations
watch -n 2 'ss -lntp | grep :443'— Observe port 443 statuswatch -d -n 1 'df -h /var'— Observe changes in disk usagewatch -n 5 'pgrep -c -f queue:work'— Observe changes in worker count
💬 "Tell me examples I can use even with an unprivileged account"
Here are common situations on live production servers, arranged in order.
Step 1 · Start Observing
Wrap the command you want to check with watch.
watch -n 2 'ss -lntp | grep :443'
Step 2 · Highlight Changes
Use -d to highlight differences.
watch -d -n 1 'df -h /var'
Step 3 · Exit
When you're done, press Ctrl+C to exit.
Ctrl+C
How It Is Used in Practice
1. Observing Connection Count During Deployment
Situation — You want to see how active connections change during deployment.
watch -n 2 "ss -s | grep estab"
Result — You can monitor changes in the active connection count in real time.
Point — Compare baseline values before and after deployment to judge sudden drops or spikes.
2. Checking Disk Growth Trend
Situation — You want to know how fast logs are filling up.
watch -d -n 5 'df -h /var && du -sh /var/log/nginx'
Result — The highlighted values let you immediately sense the growth rate.
Point — Use the growth rate to decide the log rotation interval.
Things to Watch Out For
- If the interval is too short, it can cause load, so adjust -n.
- Commands inside watch go through the shell, so be careful with quoting.
- For automated decisions, use a condition-checking script rather than watch.
Actual Output Screen
This is the result of observing TCP connection status every 2 seconds.
!watch command execution result: repeated observation of connection status
Live production server
Source (HTML): https://games.aidebatehub.com/programs/linux/automation/watch.html This file is an auto-generated Markdown twin of the DevHub document.
AI Knowledge Hub
Visitor Comments