Linux watch Command
What is the watch Command
watch is a very practical command-line tool in the Linux system. It can periodically execute a specified command and display the output full-screen. This command is especially suitable for monitoring system status, file changes, or any command output that requires continuous observation.
Basic Syntax of the watch Command
watch [选项] 命令
Detailed Explanation of Common Options and Parameters
| Option | Description | Example |
|---|---|---|
-nor--interval |
Set the refresh interval (seconds), default 2 seconds | watch -n 5 date |
-dor--differences |
Highlight the differences between two refreshes | watch -d ls -l |
-tor--no-title |
Do not display the top time, command, and interval information | watch -t free -h |
-bor--beep |
Beep when the command exit code is non-zero | watch -b ping example.com |
-eor--errexit |
Stop updating and exit when a command execution error occurs | watch -e ./check_status.sh |
-gor--chgexit |
Exit when the command output changes | watch -g ls -l |
-cor--color |
Interpret ANSI color and style sequences | watch -c 'ls --color=always' |
Practical Application Examples
Monitor System Memory Usage
watch -n 1 free -h
This command refreshes every second to display current memory usage (-hparameter makes the output more readable).
Monitor Directory File Changes
watch -d 'ls -l /var/log'
Use-dThe option can highlight changes in the file list, making it ideal for monitoring log directories.
Monitor Network Connections
watch -n 0.5 'netstat -tulnp | grep 80'
This command checks network connections on port 80 every 0.5 seconds.
Monitor CPU Temperature
watch -n 2 'sensors | grep Core'
Display CPU core temperature every 2 seconds.
Advanced Usage Tips
Combining Multiple Commands
watch 'date; echo; df -h'
Use semicolons to separate multiple commands,echoUsed to add blank lines to separate output.
Monitor Processes of a Specific User
watch 'ps -u username -o pid,cmd,%cpu,%mem --sort=-%cpu'
Monitor processes of a specified user, sorted by CPU usage.
Using Pipes and Redirection
watch 'dmesg | tail -20 > /tmp/dmesg.log; cat /tmp/dmesg.log'
Although watch cannot directly handle pipes, this can be achieved through temporary files.
Notes
- Exit watch: Press
Ctrl+Cto exit the watch command - Refresh frequency: Setting too low an interval (e.g., 0.1 seconds) may cause high system load
- Command length: Overly long commands may affect readability; consider using scripts instead
- Color support: Some commands require additional parameters to maintain color output, such as
ls --color=always
Comparison of Alternatives
| Tool | Features | Applicable Scenarios |
|---|---|---|
| watch | Simple and easy to use, full-screen refresh | Need to continuously monitor command output |
| tail -f | Real-time tracking of file changes | Monitor log file growth |
| tmux + loop command | More flexible, supports multiple windows | Complex monitoring needs |
| htop | Interactive system monitoring | System resource monitoring |
Other Extensions
Linux Command Encyclopedia