25 February 2009

Waiting for box to come back

This is a quick and easy tip, for the detail-oriented! Say a computer that you're interested in is down for some reason. Maybe you've rebooted it and you want to know the second that it's back on-line. Or maybe it's someone else's box and it's down and you (again) want to know as soon as it's up. You could just run ping and watch for the responses, but that's more traffic than is needed for a box that you know is down. And, what if you know it's going to be a while?

'ping' has a lot of great options and we can use those to send fewer packets and then quit as soon as the box responds. (So, if you're getting coffee or something when it comes back on-line, you're not just pinging it all day.) Here's the solution:

$ /sbin/ping -i 4 -o 10.1.2.3 ; echo -e '\a'
PING 10.1.2.3 (10.1.2.3): 56 data bytes

--- 10.1.2.3 ping statistics ---
35 packets transmitted, 1 packets received, 97.1% packet loss
round-trip min/avg/max/stddev = 2.856/2.856/2.856/0.000 ms


The '-i' option sets the wait between packets in seconds, which would have been 1 second by default. This was for an important box that I rebooted and needed to check as soon as it came back up. If the box was going to be down for a longer and/or unknown amount of time, you could set that higher, like 30 seconds. The '-o' option is what causes ping to exit as soon as it gets one good response. Then, I added the console beep ("echo -e '\a'") after the ping so I could go about something else and still be notified. (That form works for bash and sh, at the least.)

Hey, and I saved over 100 unnecessary packets! :) I did say this was for the "detail-oriented."

Seriously, though, I don't like the default behavior of ping -- going on forever until you stop it. I use it often and get tired of pressing '<Ctrl>-c' all the time. That's why I alias 'ping' to 'ping -nc 5'. (And, also why I used the full path above, to escape my own alias.) I think people mostly use ping for two different things: to measure the network delay to another host and to check reachability or presence a host. My alias does a reasonable job for both of those cases. The present case is what I'd consider to be a special and less common usage: waiting for a host you know is normally reachable to come back on-line.

0 Comments:

Post a Comment

<< Home