0

I am trying to detect when a host has recovered from a reboot by executing:

ping -c 1 -W 1800 myhost

I was hoping that before 30 mins passes the host would successfully ping and in my script I could take some action like ssh to the host. But the host DOES boot up before the 30 mins past, which I can verify by manually running ping myhost from the command line, but the ping -c 1 -W 1800 myhost does not succeed. It just times out.

My OS is:

$ uname -a
Linux dev-001 3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 15 17:36:42 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
7
  • I would think the packet would either reach the down server's NIC or be dropped. And since no further packets are being sent, the server will never receive one while up.
    – Oskar Skog
    Jan 27, 2020 at 22:26
  • So should I remove the -c 1 from the command? Jan 27, 2020 at 22:27
  • yes ............
    – Oskar Skog
    Jan 27, 2020 at 22:29
  • I believe most of the BSDs implement the -o switch for this purpose: ping -o -t 1800 host will ping once per second, and return success as soon as one ICMP reply is received, or failure if 1800 seconds elapse without a reply.
    – Jim L.
    Jan 28, 2020 at 1:00
  • @JimL I don't see an -o option from OpenBSD - am I looking in the wrong place? Jan 28, 2020 at 19:01

1 Answer 1

4

What your command says is to send one packet (-c 1) and wait for up to 30 minutes for the reply (-W 1800).

Since the host is presumably down at the point you send the single packet, it's unlikely to be able to send a response, so ping waits until the deadline is reached and returns failure.

Unfortunately there is no easy way to "ping until success" so you have to synthesise it with a loop. This one waits for up to 1800 seconds, sending one ping every five seconds.

end=$(($(date +%s) + 1800))
while [[ $(date +%s) -lt $end ]]
do
    ping -c 1 -i 5 -w 30 -q myhost && break
done
3
  • How about if I did ping -c 1800 -W 1 myhost? Jan 27, 2020 at 22:52
  • You'd be waiting for 30 minutes. Why not try it? Jan 27, 2020 at 22:55
  • I am ... waiting 30 mins to see if it works :) Jan 27, 2020 at 22:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .