2

I am trying to build a one-line command to monitor when my Internet connectivity drops.

I'd like it so that if I run ping www.google.com indefinitely (one ping per second), if the word "timeout" exists in an output line, another command runs, but the ping command continues to run indefinitely.

I'm running this on OS X, so the other command is say fail so I can audibly hear that there is a problem if I'm not looking at the terminal window.

If it can't be a one-line command, then a bash script would be fine.

I tried this:

ping www.google.com | grep timeout ; say fail

but that only executes the say command after the user manually terminates the ping command.

Then I tried this:

if ping www.google.com | grep timeout ; then say fail ; fi

But that never executes the say command.

3

Your problem comes from the ping command that never exits.

You should make a loop that call ping for one test -c 1:

while [ true ] ; do if ping -c 1 www.google.com | grep timeout ; then say fail ; fi ; sleep 1 ; done

edit

  • I wrote a bash while loop, may be you should adapt it to your shell program (It's been a long time I play with mac os X),
  • I added some pause (one second) to prevent the loop to consume too much cpu...
  • Worked perfectly without any modifications needed in bash 3.2.57 on OS X. – Employee Jan 18 '16 at 17:08

Your Answer

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

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