Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 3 down vote accepted

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...
share|improve this answer
    
Worked perfectly without any modifications needed in bash 3.2.57 on OS X. – mike Jan 18 at 17:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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