title: Limit bash until loop to 3 retries
body: <p>I need to pause the starting of init.d/ntpd script until after the server can communicate with a timeserver on the local production network.
  When building a replacement server, using pxe kickstart on a separate local build network, the timeserver is not available.
  It is cost prohibitive to install a timeserver on each of the local build networks due the number of remote sites (140+).
  Building a server requires the server reboot multiple times during the post_install process to ensure proper installation of multiple custom applications and custom hardware drivers.
  Without a timeserver on the build network the init.d/ntpd script continually hangs on the first reboot of the server during the post_install process.
  To keep it from hanging during the build process I'm trying to develop a method to bypass loading the init.d/ntpd script.
  Part of the kickstart process places a text file in the / folder during the kickstart process.
  I look for the /build-status.txt file during the boot process to determine if the server is still on the build network.
  If the file is there, the init.d/ntpd script looks for one of the production network switches (x.x.1.1 or x.x.2.1 or x.x.1.2) to verify that it is still connected to the build network.
  If it successfully pings one of the network switches in less than 3 attempts it removes the /build-status.txt file and then looks for one of the timeservers.
  After 3 failed attempts to ping a production network switch, the server needs to stop looking and exit the if statement to continue through the rest of the init.d/ntpd script.
  This is what I currently have, but for some reason, sshd is either, failing to enable or is being disabled, and I am unable to ssh into the server.
  Any assistance would be greatly appreciated.</p>
  
  <pre><code># Verify processor is not building
  file="/build-status.txt"
  sleepcount=0
  if [ ! -f "$file" ] ; then
      # Processor not building, wait for timeserver(s) to be available
      /bin/echo "==============================" &gt;&gt; /var/log/ntpd.log
      /bin/echo "$(date) Looking for Timeserver" &gt;&gt; /var/log/ntpd.log
      until /bin/ping -c 1 timeserver1 &gt; /dev/null || /bin/ping -c 1 timeserver2 &gt; /dev/null || /bin/ping -c 1 timeserver3 &gt; /dev/null ; do
          sleep 2
      done
      /bin/echo "$(date) Timeserver Found" &gt;&gt; /var/log/ntpd.log
      /bin/echo "==============================" &gt;&gt; /var/log/ntpd.log
  else 
      # Processor building or just finished building, wait for switch(es) to be available
      /bin/echo "==============================" &gt;&gt; /var/log/ntpd.log
      /bin/echo "$(date) Looking for Switches" &gt;&gt; /var/log/ntpd.log 
      until /bin/ping -c 1 192.168.1.1 &gt; /dev/null || /bin/ping -c 1 192.168.2.1 &gt; /dev/null || /bin/ping -c 1 192.168.1.2 &gt; /dev/null
      do
          sleep 2
          sleepcount=$((sleepcount+1))
          if [[ sleepcount -gt 3 ]] ; then
              break
          fi
      done
      if [[ sleepcount -le 3 ]] ; then
          # Processor not building, remove build-status.txt
          /bin/echo "$(date) Found Switches" &gt;&gt; /var/log/ntpd.log 
          /bin/rm -f $file
          /bin/echo "$(date) Removed $file" &gt;&gt; /var/log/ntpd.log 
          # wait for timeservers to be available
          /bin/echo "$(date) Looking for Timeserver" &gt;&gt; /var/log/ntpd.log
          until /bin/ping -c 1 timeserver1 &gt; /dev/null || /bin/ping -c 1 timeserver2 &gt; /dev/null || /bin/ping -c 1 timeserver3 &gt; /dev/null ; do
              sleep 2
          done
          /bin/echo "$(date) Timeserver Found" &gt;&gt; /var/log/ntpd.log
          /bin/echo "==============================" &gt;&gt; /var/log/ntpd.log
      else
      # Processor building
          /bin/echo "$(date) Switches NOT Found" &gt;&gt; /var/log/ntpd.log
          /bin/echo "$(date) Build in Progress" &gt;&gt; /var/log/ntpd.log
          /bin/echo "==============================" &gt;&gt; /var/log/ntpd.log         
      fi
  fi
  </code></pre>