How to start Safari on a browser page only when Error 502 stops happening?
A "quick and dirty" script to accomplish what you are looking to do could be written as:

#!/bin/bash

url = "http://url"
code=`curl -I $url 2&gt;/dev/null | head -n 1 | cut -d$' ' -f2`

while [ "$code" == 502 ]
do
  sleep 5
  echo trying again
  code=`curl -I $url 2&gt;/dev/null | head -n 1 | cut -d$' ' -f2`
done

open -a Safari $url


Basically, what it does is caputure the value that you output in a variable called code.  It then evaluates that variable in a While/Do loop every 5 seconds until the value changes.  Once that happens, it exists the loop and launches a Safari instance pointing to the URL.