0

I have this shell script which keeps running continously.

#!/bin/sh
while true;
 do ping -c1 www.google.com > /dev/null;
done

When I disconnect my lan cable this script throws error in the terminal

ping: unknown host www.google.com

but it takes some time to throw the error.

I want it to throw the error instantly even if I disconnect my lan cable for a few milliseconds and then connect it quickly. How do I do it?

node_man
  • 101
  • 2

1 Answers1

0

I resolved the issue by using this statement :

ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` > /dev/null && echo ok || echo error

This statement pings the default gateway and gives an error even if there is a slightest issue in network

Refer this answer for a detailed explaination

node_man
  • 101
  • 2