watch: Monitor command output at regular intervals
August 9th, 2024 2:00 PM Mr. Q Categories: Command
Command: watch
The watch
command is used to execute a command repeatedly at specified intervals and display the output. This is useful for monitoring changes in command output or system status in real-time.
Commands and Outputs:
- Command:
watch command
Description: Execute the specifiedcommand
repeatedly and display the output, updating every 2 seconds by default. Sample Command and Output:
$ watch df -h
Output:
Every 2s: df -h <date and time>
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 20G 28G 43% /
tmpfs 2.0G 1.0M 2.0G 1% /run
/dev/sdb1 100G 40G 55G 42% /data
- Command:
watch -n seconds command
Description: Execute the specifiedcommand
everyseconds
instead of the default 2-second interval. Sample Command and Output:
$ watch -n 5 'ls -l /var/log'
Output:
Every 5s: ls -l /var/log <date and time>
total 1024
-rw-r--r-- 1 root root 1234 Aug 9 10:00 syslog
-rw-r--r-- 1 root root 5678 Aug 9 11:00 kern.log
-rw-r--r-- 1 root root 9101 Aug 9 12:00 auth.log
- Command:
watch -d command
Description: Highlight changes between updates. The-d
flag makes the differences between updates visible. Sample Command and Output:
$ watch -d df -h
Output:
Every 2s: df -h <date and time>
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 20G 28G 43% /
tmpfs 2.0G 1.0M 2.0G 1% /run
/dev/sdb1 100G 40G 55G 42% /data
Changes will be highlighted to show differences from the previous output.
Note: Use quotes around the command if it contains spaces or special characters.