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:
This is a basic example; you’d replacewhile ($true) { Clear-Host; Get-Date; Start-Sleep -Seconds 5 }Get-Datewith your desired command.
Commands / Usage
Basic Execution
-
Watch a command every 2 seconds:
watch -n 2 'ls -l'Execute
ls -levery 2 seconds and display the output. -
Watch a command every 0.5 seconds:
watch -n 0.5 'df -h'Execute
df -hevery half second.
Display Options
-
Highlight differences between updates:
watch -d 'ps aux | grep nginx'Show
ps aux | grep nginxoutput, highlighting changed lines. -
Continuous highlighting of differences:
watch -dd 'cat /var/log/syslog | tail -n 10'Show
cat /var/log/syslog | tail -n 10output, continuously highlighting all changed parts of the output. -
Add time to the header:
watch -t 'uptime'Execute
uptimewithout showing the header (time, command, interval). -
Add command to the header:
watch -c 'netstat -tulnp'Execute
netstat -tulnpand 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.txtonce 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/statusrepeatedly. Exit automatically when the output changes from the previous execution.
Other Options
-
Specify the terminal width:
watch -w 120 'top -bn1'Execute
top -bn1and 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
python3as 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.txtrepeatedly. Once the file exists andlsoutputs something (even an error if the file disappears immediately),watchwill exit if the output changes from the initial state. A more robust way to wait for a file might involvefindorinotifywait.
Gotchas
-
Shell Interpretation: The command given to
watchis executed by/bin/shby default. If you need a different shell or complex shell features, use the-xflag to specify the interpreter or wrap your command inbash -c '...'.watch -x bash -c 'for i in {1..5}; do echo $i; sleep 1; done' -
Signals:
watchignores 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:
watchattempts to use the current terminal size. If the output of your command exceeds the terminal width, it will wrap. The-wflag can explicitly set the width. -
Output Buffering: Some commands might buffer their output. This can lead to
watchnot showing changes as frequently as expected. You might need to force unbuffered output for the command (e.g., usingstdbuf -o0 your_command). -
Exit Codes:
watchitself 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). -
-gBehavior: The-gflag (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,-gmight not trigger an exit.