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?

4 Answers

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).

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

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.

3

Another possibility is to use homebrew package-manager. See: HomebrewIf you have installed it, the way to go would be a simple:

brew install watch

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like