Skip to content

Instantly share code, notes, and snippets.

@hebertluiz
Last active October 14, 2022 20:33
Show Gist options
  • Save hebertluiz/55d40a8d44fa09142698797f3f37d727 to your computer and use it in GitHub Desktop.
Save hebertluiz/55d40a8d44fa09142698797f3f37d727 to your computer and use it in GitHub Desktop.
Wait for host up before doing stuff Shellscript | Linux
target_host='google.com'
interval=2
printf '\nWaiting for host %s' "${target_host}"
while true; do
if ping -c 2 -i .1 -W .5 ${target_host} > /dev/null; then
printf '\rHost is %s up. Time to continue' "${target_host}"
break
fi
## time to wait
sleep ${interval:-2}
printf '\rWaiting for host %s' "${target_host}"
done
### Do stuff here
# DST_PORT=443
# DST_PROTO='https'
http_response_ok='200'
target_host='google.com'
interval=2
printf '\nWaiting for host %s' "${target_host}"
while true; do
curl_ret="$(curl -k -o -I -L -s -w "%{http_code}" ${DST_PROTO:-http}://localhost:${DST_PORT:-80})"
if [ ${curl_ret} -eq ${http_response_ok:-200} ] ; then
printf '\rHost is %s up. Time to continue\n' "${target_host}"
break
fi
## time to wait
sleep ${interval:-2}
printf '\rWaiting for host %s' "${target_host}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment