Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Linux watch Command
The watch command in Linux is a powerful tool that allows you to execute a program periodically, showing output in fullscreen. This command runs the specified command repeatedly, displaying its output and errors. By default, the specified command runs every two seconds and watch runs until interrupted. In this article, we will explore the various options and uses of the watch command.
Syntax
The basic syntax of the watch command is ?
watch [options] command
Common Options
| Option | Description |
|---|---|
-d, --differences |
Highlights differences between successive updates |
-n, --interval seconds |
Specifies update interval (minimum 0.1 seconds) |
-p, --precise |
Attempts to run commands at precise intervals |
-t, --no-title |
Turns off the header and timestamp display |
-b, --beep |
Beeps if command has non-zero exit status |
-e, --errexit |
Exits on command error after keypress |
-g, --chgexit |
Exits when command output changes |
-c, --color |
Interprets ANSI color and style sequences |
Basic Examples
Monitoring System Resources
Monitor memory usage every second ?
watch -n 1 free -h
Watch CPU usage with top ?
watch -n 2 'ps aux | head -10'
Monitoring Log Files
Watch the last 10 lines of system log ?
watch -n 1 'tail -10 /var/log/syslog'
Monitoring Disk Usage
Track disk space with highlighted changes ?
watch -d df -h
Advanced Usage
Highlighting Changes
The -d option highlights differences between updates, making it easier to spot changes ?
watch -d 'ls -la /tmp'
Custom Update Intervals
Set a custom refresh interval (minimum 0.1 seconds) ?
watch -n 0.5 date
Exit on Changes
Exit automatically when output changes using -g ?
watch -g 'ls -la /var/log/messages'
Clean Output
Remove header for cleaner display ?
watch -t -n 1 uptime
Practical Use Cases
Process monitoring ? Track specific processes or system load
File system changes ? Monitor directory contents or file sizes
Network monitoring ? Watch network connections or interface statistics
Service status ? Monitor service states and system health
Build processes ? Track compilation progress or test results
Complex Command Example
Monitor network connections with color and change highlighting ?
watch -d -c -n 1 'netstat -tuln | head -20'
Tips for Effective Usage
Use quotes around complex commands with pipes or redirections
Combine with
grepto filter specific informationPress Ctrl+C to exit watch
Use appropriate intervals to balance between real-time monitoring and system load
Conclusion
The watch command is an essential Linux utility for real-time monitoring of system resources, processes, and files. Its flexibility with various options like interval control, change highlighting, and exit conditions makes it invaluable for system administration and troubleshooting tasks.
