YouTip LogoYouTip

Linux Comm Watch

# Linux watch Command [![Image 3: Linux Command Manual](#) Linux Command Manual](#) * * * ## What is the watch command watch is a very practical command-line tool in Linux systems. It can execute specified commands periodically and display the output results in full screen. This command is particularly suitable for monitoring system status, file changes, or any command output that needs continuous observation. * * * ## Basic Syntax of watch Command watch command * * * ## Common Options and Parameters Explained | Option | Description | Example | | --- | --- | --- | | `-n` or `--interval` | Set refresh interval (seconds), default is 2 seconds | `watch -n 5 date` | | `-d` or `--differences` | Highlight differences between two refreshes | `watch -d ls -l` | | `-t` or `--no-title` | Do not display header with time, command and interval | `watch -t free -h` | | `-b` or `--beep` | Beep if command has a non-zero exit status | `watch -b ping example.com` | | `-e` or `--errexit` | Stop updating and exit if command has an error | `watch -e ./check_status.sh` | | `-g` or `--chgexit` | Exit when command output changes | `watch -g ls -l` | | `-c` or `--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 (the `-h` parameter makes the output more readable). ### Monitor Directory File Changes watch -d 'ls -l /var/log' Using the `-d` option can highlight changes in the file list, which is very suitable for monitoring log directories. ### Monitor Network Connections watch -n 0.5 'netstat -tulnp | grep 80' This command checks the network connection on port 80 every 0.5 seconds. ### Monitor CPU Temperature watch -n 2 'sensors | grep Core' Displays CPU core temperature every 2 seconds. * * * ## Advanced Usage Tips ### Combine Multiple Commands watch 'date; echo; df -h' Use semicolons to separate multiple commands, `echo` is used to add blank lines to separate output. ### Monitor Specific User Processes watch 'ps -u username -o pid,cmd,%cpu,%mem --sort=-%cpu' Monitor processes of a specified user, sorted by CPU usage. ### Use Pipes and Redirection watch 'dmesg | tail -20 > /tmp/dmesg.log; cat /tmp/dmesg.log' Although watch cannot directly handle pipes, it can be achieved through temporary files. * * * ## Notes 1. **Exit watch**: Press `Ctrl+C` to exit the watch command 2. **Refresh frequency**: Setting too low an interval (e.g., 0.1 seconds) may cause high system load 3. **Command length**: Overly long commands may affect readability, consider using scripts instead 4. **Color support**: Some commands need additional parameters to maintain color output, such as `ls --color=always` * * * ## Alternative Solutions Comparison | Tool | Features | Applicable Scenarios | | --- | --- | --- | | watch | Simple and easy to use, full screen refresh | Need continuous monitoring of command output | | tail -f | Real-time tracking of file changes | Monitoring log file growth | | tmux + loop command | More flexible, multi-window support | Complex monitoring needs | | htop | Interactive system monitoring | System resource monitoring | * * Linux Command Manual](#)
← Linux Comm DnfLinux Comm Ifdown β†’