watch

monitoringLinux/Unix
The watch command is one of the most frequently used commands in Linux/Unix-like operating systems. watch Execute a program periodically, showing output fullscreen

Quick Reference

Command Name:

watch

Category:

monitoring

Platform:

Linux/Unix

Basic Usage:

watch [options] [arguments]

Common Use Cases

    Syntax

    watch [options] command

    Options

    Option Description
    -b, --beep Beep if command has a non-zero exit
    -c, --color Interpret ANSI color and style sequences
    -d, --differences[=permanent] Highlight changes between updates (permanent mode keeps all changes highlighted)
    -e, --errexit Exit if command has a non-zero exit status
    -g, --chgexit Exit when output from command changes
    -n, --interval seconds Seconds to wait between updates (can be a decimal number)
    -p, --precise Attempt to run command in precise intervals (default)
    -t, --no-title Turn off header showing command and interval
    -x, --exec Pass command to exec instead of 'sh -c'
    -h, --help Display help information and exit
    -v, --version Display version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the watch command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Watch the date command update every 2 seconds (default) watch date
    # Watch with a custom update interval (1 second) watch -n 1 date
    # Watch directory contents for changes watch ls -la
    # Watch system free memory watch free -m
    # Advanced Examples Advanced
    # Watch with highlighted differences from previous output watch -d 'ps aux | grep apache' # Watch with precise differences highlighted watch -d=cumulative 'ps aux | grep apache' # Watch with no title showing at the top watch -t free -m # Watch with custom interval in decimal seconds (0.5 = half a second) watch -n 0.5 date # Watch system load with continuous output and beep on command error watch -b -c uptime # Watch multiple commands by using semicolons watch 'date; echo "---"; uptime' # Watch until a specific string appears in the output watch -g 'ls -l | grep "my_file"' # Watch with custom colors preserved watch -c 'ls --color=always'

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `watch` command is a powerful utility that repeatedly runs a specified command at regular intervals and displays the output in fullscreen mode. It's particularly useful for monitoring system changes or observing the progress of ongoing operations. **Core Functionality:** 1. **Command Repetition**: watch executes the specified command periodically, providing a real-time view of changing information. 2. **Fullscreen Display**: The output is presented in a clear, fullscreen format that's easy to monitor, even from a distance. 3. **Change Detection**: With the -d flag, watch can highlight differences between updates, making changes immediately apparent. 4. **Customizable Refresh Rate**: Users can specify how frequently the command should be executed, from fractions of a second to minutes or longer. **Common Use Cases:** 1. **System Monitoring**: Administrators use watch to monitor system resources, processes, disk usage, or network connections in real-time. ``` watch -n 5 'free -m' # Monitor memory usage watch -n 2 'ps aux | grep apache' # Monitor Apache processes watch -n 10 'df -h' # Monitor disk space ``` 2. **Progress Tracking**: watch helps track the progress of long-running operations like file transfers, directory synchronization, or log file growth. ``` watch -n 1 'ls -l backup.tar.gz' # Watch file size growth watch -n 5 'wc -l /var/log/syslog' # Monitor log file growth ``` 3. **Network Monitoring**: It's useful for observing network status, connections, or traffic patterns. ``` watch -n 2 'netstat -tuln' # Monitor open ports watch -n 5 'ping -c 1 google.com' # Monitor network connectivity ``` 4. **Development and Debugging**: Developers use watch to monitor build processes, test outputs, or service status during development. ``` watch -n 2 'curl -s http://localhost:8080/status' # Monitor web service watch -n 5 'grep ERROR /var/log/application.log' # Watch for errors ``` **Technical Details:** 1. **Implementation**: watch is part of the procps-ng package in most Linux distributions. It uses the ncurses library for screen handling. 2. **Command Execution**: By default, watch runs commands via 'sh -c', which means shell features like pipes, redirections, and command substitutions are available. The -x option changes this to use exec directly. 3. **Difference Highlighting**: The -d option uses terminal capabilities to highlight changes between updates, making it easier to spot modifications. 4. **Precision Timing**: With the -p option (default in newer versions), watch attempts to maintain precise intervals between command executions, compensating for the time it takes to run the command itself. **Advanced Features:** 1. **Exit Conditions**: Options like -g (exit when output changes) and -e (exit when command fails) allow watch to be used in scripts that need to respond to specific conditions. 2. **Color Support**: The -c option preserves ANSI color sequences in the command output, useful for commands that produce colorized output. 3. **Audible Alerts**: The -b option makes watch beep when the command exits with a non-zero status, providing an audible notification of issues. **Limitations and Considerations:** 1. **Resource Usage**: Very frequent updates (small -n values) can consume significant system resources, especially for complex commands. 2. **Screen Size**: watch is designed for terminal use and works best with outputs that fit within the current terminal dimensions. 3. **Terminal Requirements**: watch requires a terminal that supports cursor positioning and screen clearing. It won't work in all environments. 4. **Command Execution Environment**: The environment in which watch executes commands may differ from the interactive shell environment, which can cause unexpected behavior in some cases. **Historical Context:** watch was originally created as a simple way to monitor changing system information in a clear, focused manner. It has become a standard tool in the Linux administrator's toolkit, offering a straightforward approach to real-time monitoring without the complexity of dedicated monitoring systems. While graphical monitoring tools have proliferated, watch remains popular due to its simplicity, versatility, and availability on virtually all Linux systems. The command has evolved over time, adding features like precise timing, difference highlighting, and color support, while maintaining its core simplicity and focus on the single task of repeatedly executing a command and displaying its output.

    Related Commands

    These commands are frequently used alongside watch or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the watch command works in different scenarios.

    $ watch
    View All Commands