13

I just figured out, that ping on a Linux platform (Ubuntu 13.10) does not timeout as described. I tried

ping -w 2 unreachable.com

and

ping -W 2 unreachable.com

but in neither case there was a timeout after 2 seconds. How can I use ping with a definite timeout? Is that possible at all? I want the command to stop after 2 seconds, regardless of any connection status.

3
  • works fine on my 13.04 install here. waits 2 seconds and then bails.
    – Marc B
    Dec 3, 2013 at 19:08
  • ping -w 2 <ip> works on one box and on an my FC14 box it is ping -t 2 <ip>
    – Barry
    Dec 3, 2013 at 19:11
  • I'm also experiencing a problem like this on NixOS.
    – WhittlesJr
    Feb 25, 2020 at 20:48

2 Answers 2

13

ping -c 5 -W 2 will send out 5 pings, waiting 2 seconds max for each of them (a total max of 10 seconds).

ping -w 5 will send out pings, but will stop after 5 seconds.

You have to be careful with name resolution: if you use a name instead of an IP address, the resolution of the name does not count into these timeouts & waits (pinging and time measurements start only after the name resolution has finished). If you use DNS, you can set DNS timeouts in /etc/resolv.conf - see its man page.

3

Are you misinterpreting the flag? If I understand correctly:

The -W flag will specify how long to wait for a reply. By setting -W 2, according to the man page:

Time to wait for a response, in seconds. The option affects only timeout in absense of any responses, otherwise ping waits for two RTTs

So running it like you have and waiting for 2 seconds doesn't actually let you know if it has given up waiting for the response or not.

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