watch Repeat Command

watch cheatsheet — run any command repeatedly and display output full-screen. watch -n 2 df -h, watch kubectl get pods, watch -d to highlight changes. Live command monitoring.

4 min read

What it is

Periodically execute a command and display its output fullscreen, updating at a set interval. Useful for monitoring changes in real-time.

Installation

Linux:

sudo apt update && sudo apt install watch # Debian/Ubuntu
sudo yum install watch # Fedora/CentOS/RHEL
sudo dnf install watch # Newer Fedora

macOS:

brew install watch

Windows: watch is not a native Windows command. You can achieve similar functionality using PowerShell or third-party tools.

  • PowerShell:
    while ($true) { Clear-Host; Get-Date; Start-Sleep -Seconds 5 }
    
    This is a basic example; you’d replace Get-Date with your desired command.

Commands / Usage

Basic Execution

  • Watch a command every 2 seconds:

    watch -n 2 'ls -l'
    

    Execute ls -l every 2 seconds and display the output.

  • Watch a command every 0.5 seconds:

    watch -n 0.5 'df -h'
    

    Execute df -h every half second.

Display Options

  • Highlight differences between updates:

    watch -d 'ps aux | grep nginx'
    

    Show ps aux | grep nginx output, highlighting changed lines.

  • Continuous highlighting of differences:

    watch -dd 'cat /var/log/syslog | tail -n 10'
    

    Show cat /var/log/syslog | tail -n 10 output, continuously highlighting all changed parts of the output.

  • Add time to the header:

    watch -t 'uptime'
    

    Execute uptime without showing the header (time, command, interval).

  • Add command to the header:

    watch -c 'netstat -tulnp'
    

    Execute netstat -tulnp and include the command and interval in the header.

Execution Control

  • Run the command only once:

    watch -n 1 -g 'cat /tmp/new_file.txt'
    

    Execute cat /tmp/new_file.txt once and exit immediately if the output hasn’t changed from the first run.

  • Exit when the command’s output changes:

    watch -g 'curl -s http://example.com/status'
    

    Execute curl -s http://example.com/status repeatedly. Exit automatically when the output changes from the previous execution.

Other Options

  • Specify the terminal width:

    watch -w 120 'top -bn1'
    

    Execute top -bn1 and force the output to be 120 characters wide.

  • Change the interpreter:

    watch -x python3 -c 'import time; print(int(time.time()))'
    

    Execute the Python command using python3 as the interpreter, updating every default interval.

Common Patterns

  • Monitor disk space usage:

    watch -n 5 'df -h /'
    

    Check the disk usage of the root partition every 5 seconds.

  • Monitor a specific process:

    watch -n 2 'ps aux | grep your_process_name'
    

    Keep an eye on a running process, updating every 2 seconds.

  • Monitor network connections:

    watch -n 3 'sudo netstat -tulnp | grep :80'
    

    Watch for any processes listening on port 80 every 3 seconds.

  • Monitor file changes (simple):

    watch -n 1 'ls -lt /var/log/myapp/'
    

    See the latest files in /var/log/myapp/ as they appear, sorted by modification time.

  • Monitor log file tail:

    watch -n 1 'tail -n 5 /var/log/nginx/access.log'
    

    Continuously display the last 5 lines of an Nginx access log.

  • Monitor system load:

    watch -n 2 'uptime'
    

    Get a quick look at system load averages and uptime every 2 seconds.

  • Monitor HTTP status:

    watch -n 10 'curl -s http://localhost:8080/health'
    

    Check the health endpoint of a web service every 10 seconds.

  • Watch for a file to appear and then exit:

    watch -g 'ls /path/to/expected_file.txt'
    

    This will run ls /path/to/expected_file.txt repeatedly. Once the file exists and ls outputs something (even an error if the file disappears immediately), watch will exit if the output changes from the initial state. A more robust way to wait for a file might involve find or inotifywait.

Gotchas

  • Shell Interpretation: The command given to watch is executed by /bin/sh by default. If you need a different shell or complex shell features, use the -x flag to specify the interpreter or wrap your command in bash -c '...'.

    watch -x bash -c 'for i in {1..5}; do echo $i; sleep 1; done'
    
  • Signals: watch ignores SIGINT (Ctrl+C) by default, meaning Ctrl+C might not immediately stop it. You might need to press Ctrl+C multiple times or send a SIGKILL.

  • Terminal Size: watch attempts to use the current terminal size. If the output of your command exceeds the terminal width, it will wrap. The -w flag can explicitly set the width.

  • Output Buffering: Some commands might buffer their output. This can lead to watch not showing changes as frequently as expected. You might need to force unbuffered output for the command (e.g., using stdbuf -o0 your_command).

  • Exit Codes: watch itself doesn’t directly report the exit code of the executed command. It will keep running unless you explicitly tell it to exit based on output changes (-g).

  • -g Behavior: The -g flag (difference highlighting) exits when the output changes from the previous output. If the command itself fails or produces an error, and that error message is consistent, -g might not trigger an exit.