ChatGPT解决这个技术问题 Extra ChatGPT

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?

because I'd have to configure all the Macs in the office that user my script
brew's watch doesn't seem to pick up commands on my path or aliases.
Alternatively to a command you could also use the application /Applications/Utilities/Console.app to monitor log files. It allows filtering with basic search
Doesn't this belong to apple.stackexchange.com?
You can use a package manager like brew.

s
seantomburke

With Homebrew installed:

brew install watch


Definitely the way to go if you use brew on mac.
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/…
This watch from brew doesn't seem to read the user aliases. When executing watch somealias, I get a command not found.
@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.
Given the simplicity of the accepted answer, in my view your answer is 1. an overkill 2. hide detail implementations in a sense that it makes others think watch command is something super hard to implement.
p
pjvandehaar

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

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
This works pretty well, but I prefer sleep ${2:-1} to make the interval optional.
Also, it's echo "$(date)" equivalent to date?
It also doesn't work with pipes. Here's a fork with various improvements: daniel.lubarov.com/simple-watch-script-for-osx
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)
P
Peter Mortensen

Use MacPorts:

$ sudo port install watch

There is no better way then using the watch command directly.
Why use brew instead of MacPorts?
@bmauter, to each their own: stackoverflow.com/questions/21374366/…
@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?
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.
P
Peter Mortensen

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

Why do you prefer an alias to a function if you're wrapping it in a function anyway?
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.
g
ghoti

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

Unfortunately this shell script doesn't work with bash expanded parameters such as: watch 2 cat *
@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 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. :)
@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, 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. :)
P
Peter Mortensen

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


easier now even brew install watch
A
A_funs

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


Upvote for visionmedia-watch - it supports colors by default. watch doesn't, with or without --color option.
P
Peter Mortensen

Or, in your ~/.bashrc file:

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

P
Peter Mortensen

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

Should be echo -e instead of just echo, to render those line breaks correctly
Thanks to both of you guys, this is a great answer for client machines that restrict the install of package managers like Brew.
In fact you can reduce flickering further by executing your command, then executing 'date' and capturing that o/p, and then clearing...
A
AstroCB

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

C
Community

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

P
Peter Mortensen

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