Is there a native alternative to the `watch` command; for Darwin/OS X

I have a script that I'm trying to port from Linux to Darwin/OS X. The Linux version currently depends on the watch command, which doesn't appear to be installed on Darwin/OS X by default. What's the native alternative?


Solution 1:

There's no exact replacement. You could install the Linux watch command with your favorite package manager (homebrew or macports), or manually.

Or you could roll your own watch-like functionality with a small loop inside a shell function, like:

fakewatch () { while true; do clear; date; ${1}; sleep 2; done; }

Then call fakewatch ps (replace ps with whatever command you want to watch).

Solution 2:

Based on @Spiff awesome answer, I improved it a bit to avoid flickering/flashing on each execution:

fakewatch () { while true; do DATE=$(date); RESULT=$(${@}); clear; echo "$DATE"; echo "$RESULT"; sleep 5; done; }

This way, we store the result before printing it so at the time of printing everything gets printed at the same time

Solution 3:

This question Is there a way to dynamically refresh the less command? might help.

For OS that doesn't support watch (like Darwin/OS X, Solaris), you can try

less +F FILENAME

It's equivalent to pressing Shift+F after less FILENAME which keep refreshing the file.