Skip to content

Instantly share code, notes, and snippets.

@erlend
Last active September 25, 2021 02:03
Show Gist options
  • Save erlend/547f2de1c60e0c832a1da928a677e549 to your computer and use it in GitHub Desktop.
Save erlend/547f2de1c60e0c832a1da928a677e549 to your computer and use it in GitHub Desktop.
#!/bin/sh
if [ -z "$1" ]; then
cat <<-END_OF_HELP
wait_for
Usage:
wait_for FILE
Checks for existance of file repeatedly until it succeeds then it exits. File
can also be a couple of predefined commands (spring and bundler), TCP port or
http/https URL.
END_OF_HELP
exit 1
fi
action=$1
attempts=1
case $action in
spring)
set -- "bin/spring status 2>/dev/null | grep -q 'Spring is running'"
;;
bundler)
set -- "bin/bundle check"
;;
http://* | https://*)
set -- wget --spider "$@"
;;
*:*)
host=$(echo "$@" | cut -d: -f1)
port=$(echo "$@" | cut -d: -f2)
set -- nc -z "$host" "$port"
;;
*)
if which "$1" >/dev/null 2>&1; then
set -- "$@"
else
set -- test -f "$1"
fi
;;
esac
printf "\nWaiting for %s\n" "$action"
while ! "$@" > /dev/null 2>&1; do
if [ $attempts = "${WAIT_FOR_ATTEMPTS:-0}" ]; then
echo " Failed after $attempts attempts!"
exit 1
fi
attempts=$((attempts + 1))
printf "."
sleep "${WAIT_FOR_TIME:-1}"
done
printf " Succeded after %s attempts!\n" "$attempts"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment