Intermediate LevelLinux Commands & SysAdmin
How does the watch command execute commands repeatedly at fixed intervals (watch -n 2 df -h)?
❓ Interview Prompt: Explain watch command, execution interval (-n), highlighting changes (-d), and monitoring system state.
The 'watch' command executes a specified CLI command periodically at fixed time intervals, displaying the output on a full-screen terminal window:
Options:
•
'-n 2': Set update interval in seconds (default is 2 seconds).
•
'-d': Highlight differences between consecutive updates.
Code Implementation Example
bash
# Monitor free memory and highlighting changes every 1 second
watch -n 1 -d free -h
# Monitor active network connections on port 80
watch -n 2 "ss -tulpn | grep :80"💡Periodically executing CLI commands using watch -n.
🎯 Interview Key Takeaway
Use watch -n <sec> -d to monitor changing command outputs (memory, disk, connections) in real-time.
#watch#CLI Monitoring#SysAdmin#Intermediate
Back to all Linux Commands & SysAdmin Questions→