Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OS X Bash, 'watch' command

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?

like image 624
joseph.hainline Avatar asked Mar 05 '12 21:03

joseph.hainline


People also ask

How do you use the watch command?

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.

How do you escape the watch command?

To exit the watch command, we press Ctrl-C.

How do you stop a watch command in Linux?

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.


2 Answers

With Homebrew installed:

brew install watch

like image 118
seantomburke Avatar answered Oct 31 '22 18:10

seantomburke


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 
like image 28
Daniel Pittman Avatar answered Oct 31 '22 18:10

Daniel Pittman