From the L.U.S.T Mailing List on 07 Aug 1998
#!/path/to/perl $ping = `ping -c 1 10.10.10.10`; exec ("program") if $ping =~ /100\% packet loss/;
What's wrong with a simple:
ping -c 1 $target && $do_something $target || $complain
... where you fill $do_something and $complain with commands that you actually want to run on success or failure of the 'ping'.
That's what shell "conditional execution operators" (&& and ||) are for after all.
or something similar with a shell script... or, a quick socket program (probably a little easier on the system)
john
I don't know why any other socket operations would be "easier on the system" than a single 'ping' (ICMP echo request).
> Hi,
> I'm looking for a program that can ping a host, and based on whether or not
> the host is unreachable execute a program. Anyone know of something like
> this, (or how to write one...)?
> Thanks for any help.
> -Corey
From L.U.S.T List on 12 Aug 1998
Re a command like:
ping -c 1 $host && do_if_up || do_if_unreachable_or_down
> The orginal poster asked a very simple question with
> a very simple answer. He (or she) did not go into any
> details about his (or her) requirements.
Jim,
Thanks for your spirited post. It made my morning... It's a sure bet that if you ask a bunch of technical guys a simple question, you're going to get about 10 different, complicated, and lengthy answers.
But, we do live in the real world, and the simplest answer is often the best. Although I liked John Lampe's perl script because of its flexibility, I think I'll be using the shell conditionals that you proposed.
In case you're curious, here's what I actually want to do... I have a web server over here, and it's pretty important that it remains up. PC's are cheap, so why buy one when you can have two for twice the price? I use IP aliasing for my important machine names, "www" "mail" etc. I'd like the backup machine to ping the primary machine. Should the primary machine stop responding, I'd like the backup machine to run another script and pick up the important aliases. As soon as the primary machine goes back up, the secondary machine will drop the aliases and go back to its "waiting" state...
The only part of this little mess I didn't know how to do was execute a script based on the result of a ping... So that was all that I asked. Maybe next time I'll just lay out the whole thing so nobody starts guessing.
Anyhow, thanks to everyone who offered advice. I'm now able to complete this project.
-Corey
This set of requirements is pretty common --- common enough to have a name: "failover"
I'd suggest that you assign each of these two systems two IP addresses (one on each can be from RFC1918 --- something like 192.168.1.*). We'll call that the "control address" and the one that the web server is on the "service address." Now, when you detect a failure on the service address you take it over (address assumption). You can then get messages from the control address to let your failover host know that the other system is back up and running --- which is when you relinquish control of that address.
Naturally you can expect some discontinuities in sessions that were running at failover point. Luckily normal HTTP is pretty robust and stateless --- so it should be O.K. for that. If you are running complex systems of CGI scripts which maintain state via local temp files, you might have some problems with this simple failover approach.
Look for the "High Availability HOWTO" for some other ideas on this.
In addition I recommend that you look at the panic= kernel parameter and that you consider running your web servers out of your inittab (so that 'init' will automatically respawn them as necessary). You can also consider configuring the built-in watchdog support (re-compile your kernel) and even installing a hardware watchdog timer card.
A WDT (watchdog timer) is basically a "deadman's switch" for your computer. Once initialized it must be updated periodically (by the kernel or some daemon) or it will trigger the reset line on your system bus.