Skip to content
Advertisement

How to ping in linux until host is known with X seconds timeout?

Im trying to wait for a device to boot up in my code and i dont want to use sleep to wait for him. my problem is that sometimes the device fail to boot and im stuck in a loop when using:

JavaScript

How can i try to ping to the device for X seconds and print “DEAD” or “ALIVE” using one liner?

Advertisement

Answer

You need -W option that keeps ping waiting for a timeout of X seconds.

For Linux (iputils):

JavaScript

For MacOS X:

JavaScript

Not a one-liner, but this would wait for 10 seconds before timing out:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement