Script hacks: waiting for the internet

Now and then the VMs (kvm, libvirt + vmbuilder) I was cranking out would start up too fast, and the “first boot” script would run before the host got an IP address and had internet access. Since the first thing I was doing was downloading the Rubygems source using wget (to install chef), and since wget lacks a retry for dns failure, I hacked up this script to wait for the internet a bit.

#!/bin/bash

# Wait for internet to come up (DHCP)
MAXWAIT=60
WAITTIME=0
host production.cf.rubygems.org > /dev/null

while [ $? == 1 ] && [ $WAITTIME -le $MAXWAIT ] ; do
  WAITTIME=$(($WAITTIME + 10))
  sleep 10
  echo -n .
  host production.cf.rubygems.org > /dev/null
done

1 thought on “Script hacks: waiting for the internet

  1. Raymond

    Aside from poor shell scripting practices, you can also look at /etc/resolv.conf to determine if it has been updated. Also you can use the ip command to check for an IP address being allocated to your network interface and that the routing table is setup correctly.

    #!/bin/sh

    # Wait for internet to come up (DHCP)
    MAXWAIT=60
    WAITTIME=0
    STEPTIME=10
    HOSTNAME=”production.cf.rubygems.org”

    while ! $(host “${HOSTNAME}” > /dev/null 2>&1 || [ “${WAITTIME}” -le “${MAXWAIT}” ]) ; do
    WAITTIME=$((WAITTIME+STEPTIME))
    sleep “${STEPTIME}”
    echo -n .
    done

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.