A host-ping test that always works correctly from the interactive bash
command line in Cygwin, is behaving incorrectly in its own crontab--always selecting the second host--and I can't figure out why:
SHELL=/bin/bash
*/29 7-23 * * * [ -n "$(pidof unison)" ] || (partner=5.174.63.120; ping -A -c5 $partner 6 7 |grep -w "ttl" || partner=5.3.172.247; time nice unison-sync $partner &> /tmp/sync.master.dev.log )
which ping
/usr/bin/ping
This basically wants 5.174.63.120 to be the 1st choice sync host preference, however if it doesn't ping back at the moment then use 5.3.172.247 instead for this round. If unison
is not already running, that is.
Yet running from the command-line always works as expected, echoing the first IP address if available, else the second:
partner=5.174.63.120; ping -A -c5 $partner 6 7 |grep -w "ttl" || partner=5.3.172.247; echo $partner
In Ubuntu this works both in crontab
and cli.
Is there a better way I can accomplish this, that is both compact and still portable on both my OSes (Linux Ubuntu 11.10 and Cygwin under Windows7,32bit)?
Even better, I'd like to generalize my host-checking to more that two, but still code it into a concise crontab line:
Use A if reachable, else use B if reachable, else use C if reachable, ... else just use Z.
ping -A -c5 $partner || partner=nnn
however I needed to add the6 7
and|grep ...
for Cygwin compatibility.