472

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?

6
  • 3
    because I'd have to configure all the Macs in the office that user my script Commented Mar 5, 2012 at 21:27
  • brew's watch doesn't seem to pick up commands on my path or aliases. Commented Apr 10, 2014 at 5:02
  • Alternatively to a command you could also use the application /Applications/Utilities/Console.app to monitor log files. It allows filtering with basic search Commented Feb 27, 2015 at 14:22
  • Doesn't this belong to apple.stackexchange.com? Commented May 23, 2016 at 9:00
  • You can use a package manager like brew.
    – Secko
    Commented Aug 18, 2016 at 20:37

14 Answers 14

901

With Homebrew installed:

brew install watch
8
  • 48
    Definitely the way to go if you use brew on mac.
    – Maxence
    Commented Nov 12, 2014 at 16:24
  • 4
    More details would be appreciated. When I search "brew watch osx", this is the top search result. EDIT: Apparently it's a Linux command. For those who weren't sure how to use watch: stackoverflow.com/questions/13593771/…
    – jds
    Commented Mar 29, 2016 at 18:16
  • 6
    This watch from brew doesn't seem to read the user aliases. When executing watch somealias, I get a command not found. Commented Mar 31, 2016 at 19:21
  • 2
    @NickMc the proper way to use watch is to do watch [command] when you use watch somealias it is assuming that somealias is a command that you can run in your terminal. Commented Apr 2, 2016 at 0:07
  • 4
    This and the accepted answer are both good. If you run man watch after install, you'll see it has more capabilities than a simple command/script, starting with a default duration and an optional switch (-n). If avoiding dependencies is a priority the accepted answer is great, but otherwise brew install watch takes minimal effort and gives you the capability you might be used to if using Linux.
    – fazy
    Commented Jul 29, 2021 at 15:23
451

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
11
  • 3
    I believe watch only shows the first screenful of output. If you want to do something similar, change your_command to your_command 2>&1|head -10 Commented Nov 10, 2013 at 15:36
  • 13
    This works pretty well, but I prefer sleep ${2:-1} to make the interval optional. Commented Nov 20, 2013 at 10:13
  • 3
    Also, it's echo "$(date)" equivalent to date? Commented Nov 27, 2013 at 5:02
  • 5
    It also doesn't work with pipes. Here's a fork with various improvements: daniel.lubarov.com/simple-watch-script-for-osx Commented Nov 27, 2013 at 5:11
  • Doesn't work like watch when the output is more than one screen. Only shows the end, while watch only shows the top. (Neither is ideal)
    – h__
    Commented Feb 5, 2014 at 8:20
44

Use MacPorts:

$ sudo port install watch
6
  • 3
    There is no better way then using the watch command directly.
    – Oliver
    Commented Aug 25, 2013 at 10:05
  • 11
    Why use brew instead of MacPorts?
    – bmauter
    Commented May 27, 2015 at 14:41
  • 2
    @bmauter, to each their own: stackoverflow.com/questions/21374366/… Commented Nov 2, 2015 at 4:50
  • 5
    @CenterOrbit, that's precisely why I asked. @joseph.hainline says don't use MacPorts, but doesn't state why. Is the watch command busted in MacPorts?
    – bmauter
    Commented Nov 2, 2015 at 19:46
  • 5
    I never recovered emotionally from using MacPorts. It ended up screwing up my system so thoroughly that I had to completely reformat my hard drive. Homebrew all the way--I challenge you to find something prevalent that's on ports that's not on HB.
    – mpowered
    Commented Feb 7, 2019 at 2:45
22

The shells above will do the trick, and you could even convert them to an alias (you may need to wrap in a function to handle parameters):

alias myWatch='_() { while :; do clear; $2; sleep $1; done }; _'

Examples:

myWatch 1 ls ## Self-explanatory
myWatch 5 "ls -lF $HOME" ## Every 5 seconds, list out home directory; double-quotes around command to keep its arguments together

Alternately, Homebrew can install the watch from http://procps.sourceforge.net/:

brew install watch
2
  • 1
    Why do you prefer an alias to a function if you're wrapping it in a function anyway?
    – philraj
    Commented Jul 14, 2020 at 18:29
  • 1
    No intentional preference; I just have a habit of creating aliases. It is possible those habits are born from legacy shells that didn't rely on or support functions, but I can't confirm.
    – IT Gumby
    Commented Dec 2, 2020 at 21:00
19

It may be that "watch" is not what you want. You probably want to ask for help in solving your problem, not in implementing your solution! :)

If your real goal is to trigger actions based on what's seen from the tail command, then you can do that as part of the tail itself. Instead of running "periodically", which is what watch does, you can run your code on demand.

#!/bin/sh

tail -F /var/log/somelogfile | while read line; do
  if echo "$line" | grep -q '[Ss]ome.regex'; then
    # do your stuff
  fi
done

Note that tail -F will continue to follow a log file even if it gets rotated by newsyslog or logrotate. You want to use this instead of the lower-case tail -f. Check man tail for details.

That said, if you really do want to run a command periodically, the other answers provided can be turned into a short shell script:

#!/bin/sh
if [ -z "$2" ]; then
  echo "Usage: $0 SECONDS COMMAND" >&2
  exit 1
fi

SECONDS=$1
shift 1
while sleep $SECONDS; do
  clear
  $*
done
5
  • Unfortunately this shell script doesn't work with bash expanded parameters such as: watch 2 cat * Commented Aug 13, 2013 at 0:00
  • 1
    @CodeCommander - The command line watch 2 cat * would expand parameters before running the script, so in a directory with files "foo" and "bar", you'd run cat foo bar every 2 seconds. What behaviour were you expecting?
    – ghoti
    Commented Aug 19, 2013 at 14:33
  • @ghoti I'm merely pointing out that the behavior is different from the watch command on Ubuntu. The Ubuntu version apparently runs the command every two seconds (expanding the parameters each time it is run), this script has the parameters expanded before it is run, and then runs using the same parameters every two seconds. So if you want to watch files in a directory where files are being added and removed this script doesn't help. It is useful when you don't use expanded parameters though. :) Commented Aug 19, 2013 at 16:39
  • @CodeCommander, I think you are mistaken. Bash (like sh) expands * without passing it to the command you run. Try running: mkdir /tmp/zz; touch /tmp/zz/foo; watch -n 2 ls -l /tmp/zz/* in one window. While that's running, you can touch /tmp/zz/bar in another window. See if your "watch" sees the change, in the first window. I don't think it will. It doesn't for me. This has nothing to do with Ubuntu vs OSX or Linux vs Unix, it's the behaviour of bash itself.
    – ghoti
    Commented Aug 19, 2013 at 20:10
  • 1
    @ghoti, you are right. I was mistaken. Even with the Linux watch command you need to use quotes: watch "ls -l /tmp/zz/*" but if you remember to use quotes it works with your script as well. :) Commented Aug 27, 2013 at 16:05
12

I am going with the answer from here:

bash -c 'while [ 0 ]; do <your command>; sleep 5; done'

But you're really better off installing watch as this isn't very clean...

1
  • 14
    easier now even brew install watch
    – Peter Host
    Commented Jan 30, 2013 at 23:35
10

If watch doesn't want to install via

brew install watch

There is another similar/copy version that installed and worked perfectly for me

brew install visionmedia-watch

https://github.com/tj/watch

1
  • Upvote for visionmedia-watch - it supports colors by default. watch doesn't, with or without --color option. Commented May 11, 2021 at 10:40
9

Or, in your ~/.bashrc file:

function watch {
    while :; do clear; date; echo; $@; sleep 2; done
}
7

To prevent flickering when your main command takes perceivable time to complete, you can capture the output and only clear screen when it's done.

function watch {while :; do a=$($@); clear; echo "$(date)\n\n$a"; sleep 1;  done}

Then use it by:

watch istats
4
  • Should be echo -e instead of just echo, to render those line breaks correctly
    – jez
    Commented Jun 22, 2020 at 17:04
  • Thanks to both of you guys, this is a great answer for client machines that restrict the install of package managers like Brew. Commented Jun 29, 2020 at 21:27
  • 1
    In fact you can reduce flickering further by executing your command, then executing 'date' and capturing that o/p, and then clearing... Commented Oct 26, 2020 at 12:32
  • To expand on the previous comment: function watch {while :; do a=$($@); b=$(date); clear; echo "$b\n\n$a"; sleep 1; done}
    – eho
    Commented Apr 17 at 15:31
2

Try this:

#!/bin/bash
# usage: watch [-n integer] COMMAND

case $# in
    0)
        echo "Usage $0 [-n int] COMMAND"
        ;;
    *)      
        sleep=2;
        ;;
esac    

if [ "$1" == "-n" ]; then
    sleep=$2
    shift; shift
fi


while :; 
    do 
    clear; 
    echo "$(date) every ${sleep}s $@"; echo 
    $@; 
    sleep $sleep; 
done
0
1

Here's a slightly changed version of this answer that:

  • checks for valid args
  • shows a date and duration title at the top
  • moves the "duration" argument to be the 1st argument, so complex commands can be easily passed as the remaining arguments.

To use it:

  • Save this to ~/bin/watch
  • execute chmod 700 ~/bin/watch in a terminal to make it executable.
  • try it by running watch 1 echo "hi there"

~/bin/watch

#!/bin/bash

function show_help()
{
  echo ""
  echo "usage: watch [sleep duration in seconds] [command]"
  echo ""
  echo "e.g. To cat a file every second, run the following"
  echo ""
  echo "     watch 1 cat /tmp/it.txt" 
  exit;
}

function show_help_if_required()
{
  if [ "$1" == "help" ]
  then
      show_help
  fi
  if [ -z "$1" ]
    then
      show_help
  fi
}

function require_numeric_value()
{
  REG_EX='^[0-9]+$'
  if ! [[ $1 =~ $REG_EX ]] ; then
    show_help
  fi
}

show_help_if_required $1
require_numeric_value $1

DURATION=$1
shift

while :; do 
  clear
  echo "Updating every $DURATION seconds. Last updated $(date)"
  bash -c "$*"
  sleep $DURATION
done
1

Use the Nix package manager!

Install Nix, and then do nix-env -iA nixpkgs.watch and it should be available for use after the completing the install instructions (including sourcing . "$HOME/.nix-profile/etc/profile.d/nix.sh" in your shell).

0

The watch command that's available on Linux does not exist on macOS. If you don't want to use brew you can add this bash function to your shell profile.

# execute commands at a specified interval of seconds
function watch.command {
  # USAGE: watch.commands [seconds] [commands...]
  # EXAMPLE: watch.command 5 date
  # EXAMPLE: watch.command 5 date echo 'ls -l' echo 'ps | grep "kubectl\\\|node\\\|npm\\\|puma"'
  # EXAMPLE: watch.command 5 'date; echo; ls -l; echo; ps | grep "kubectl\\\|node\\\|npm\\\|puma"' echo date 'echo; ls -1'
  local cmds=()
  for arg in "${@:2}"; do
    echo $arg | sed 's/; /;/g' | tr \; \\n | while read cmd; do
      cmds+=($cmd)
    done
  done
  while true; do
    clear
    for cmd in $cmds; do
      eval $cmd
    done
    sleep $1
  done
}

https://gist.github.com/Gerst20051/99c1cf570a2d0d59f09339a806732fd3

0

No blinky blinky

newwatch(){while :; do "$@" > /tmp/newwatch; clear; cat /tmp/newwatch; sleep 1; done}

Not the answer you're looking for? Browse other questions tagged or ask your own question.