Search code examples
macosbashautomationwatch

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?


Solution

  • 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