I'm looking for the best way to duplicate the Linux 'watch' command on Mac OS X. I'd like to run a command every few seconds to pattern match on the contents of an output file using 'tail' and 'sed'.
What's my best option on a Mac, and can it be done without downloading software?
Linux watch Command Overview By default, the watch command updates the output every two seconds. Press Ctrl+C to exit out of the command output. The watch command is useful when you need to monitor changes in a command output over time. This includes disk usage, system uptime, or tracking errors.
To exit the watch command, we press Ctrl-C.
Note: “watch” won't terminate on its own. You have to manually send termination signal to stop the command from running anymore. Press “Ctrl + C” to terminate the process.
With Homebrew installed:
brew install watch
You can emulate the basic functionality with the shell loop:
while :; do clear; your_command; sleep 2; done
That will loop forever, clear the screen, run your command, and wait two seconds - the basic watch your_command
implementation.
You can take this a step further and create a watch.sh
script that can accept your_command
and sleep_duration
as parameters:
#!/bin/bash # usage: watch.sh <your_command> <sleep_duration> while :; do clear date $1 sleep $2 done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With