I'm writing on a script rebooting various Server's. After the reboot I want to "wait" until all Server's are back online. (To keep things simple I defined for me online=pingable)

So for each Server I do

ServerXY_W=1
echo -n "waiting for ServerXY ..."
while (($ServerXY_W == 1))
do
   if ping -c 1 -w 0.2 192.168.123.123 &> /dev/null
   then
      echo "ServerXY is back online!"
      ServerXY_W=0
   else
      echo -n "."
   fi
done

What I would expect (and like) would be an output like e.g.

waiting for ServerXY .................
ServerXY is back online!

where the dots .... would appear one by one.

But what actually happens is first there is only

waiting for ServerXY ...

for a while and when the Server is back I get the last dot and the last line like

waiting for ServerXY ....
ServerXY is back online!

Why is the while loop only performed twice like once with ping failing and once with ping succeeding? What do I have to change to get more dots added in the while loop?

I did the test also with a non existent IP. But it got stuck with

waiting for NonExistentServer...

and never terminated of course. But the same question why don't the ........ get added?

  • Works fine for me ... :/ – Ravexina Jun 27 '17 at 17:15
up vote 2 down vote accepted

The issue

The problem is that you've set -w 0.2. When value is below 1, deadline (-w) and timeout (-W) values are ignored. This has been mentioned previously in this question. When you use -w 1 , your script (which I slightly modified to remove useless bits) works properly:

$ ./ping_server.sh                                                 
waiting for ServerXY ....................
Server is back online

$ cat ./ping_server.sh
#!/bin/bash
printf "%s" "waiting for ServerXY ..."
while ! ping -c 1 -n -w 1 147.153.237.192 &> /dev/null
do
    printf "%c" "."
done
printf "\n%s\n"  "Server is back online"

Solution

Obvious solution is to use -w 1. If you do intend on using a value lower than 1 second, the timeout command should be better:

$ timeout 0.2 ping -c 1 147.153.237.192                            
PING 147.153.237.192 (147.153.237.192) 56(84) bytes of data.
64 bytes from 147.153.237.192: icmp_seq=1 ttl=124 time=2.61 ms

--- 147.153.237.192 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 2.612/2.612/2.612/0.000 ms

Again, use it with ! operator in the loop:

#!/bin/bash
printf "%s" "waiting for ServerXY ..."
while ! timeout 0.2 ping -c 1 -n 147.153.237.192 &> /dev/null
do
    printf "%c" "."
done
printf "\n%s\n"  "Server is back online"
  • Hi thank you very much for clearing that -w thing! Is there a different way than doing it in the loop condition? It's perfect for waiting for one server but as mentioned I'm waiting for multiple server's later I'ld do something like while (( $ServerA_W==1 || $ServerB_W==1 || .....)) which holds when every server is back. – derHugo Jun 28 '17 at 5:12
  • After e.g. one server is back waiting for the others I don't want to ping the one that is back already ;) – derHugo Jun 28 '17 at 5:20
  • I'd suggest writing that as function and launching instance of each function with ip address as argument in background. But I'd recommend against printing the dots, just let each function print message when server comes up. Let me know if you want me to write an example of such function – Sergiy Kolodyazhnyy Jun 28 '17 at 5:28
  • The dots are only the debug phase ;) later I will display all Server's in rows and update the according row when a server is back. I'm not an expert an threading stuff from a script and would prefer to be able to just hold the complete script by Ctrl C in case that e.g one server never comes back.. anyway thanks your answer already solved the question :) later I might come back with a new question facing the further stuff you are addressing – derHugo Jun 28 '17 at 5:32
  • Hi I'm testing the timout thing. Problem: a timout of 0.2 seems to short so the ping never succeeds ... though it says it is in seconds if I put 2 it is still way to fast ... – derHugo Jun 28 '17 at 9:04

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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