Skip to content

Instantly share code, notes, and snippets.

@dedeibel
Created March 27, 2017 14:10
Show Gist options
  • Save dedeibel/5ca147919ccd0b62df2ed22c0254d191 to your computer and use it in GitHub Desktop.
Save dedeibel/5ca147919ccd0b62df2ed22c0254d191 to your computer and use it in GitHub Desktop.
wait for postgresql server
#!/usr/bin/env bash
SERVICE="postgres"
DBNAME="mydb"
PORT="$1"
USER="$2"
PASS="$3"
MAX=30
IT=0
SLEEP_SECONDS=2
if [ -z "$PORT" -o -z "$USER" -o -z "$PASS" ]; then
echo "no port user or password given"
exit 1
fi
service_ready() {
PGPASSWORD="$PASS" psql -h localhost -p "$PORT" -d "$DBNAME" -U "$USER" -c 'SELECT 1;' &> /dev/null
}
echo "checking for service $SERVICE on port $PORT with user $USER"
while !(service_ready &> /dev/null); do
if [ $IT -gt $MAX ]; then
echo "service $SERVICE did not start in given timeout $MAX * ${SLEEP_SECONDS}s:"
service_ready
fi
IT=$(($IT+1))
sleep "$SLEEP_SECONDS"
echo "checking for service $SERVICE (try $IT of $MAX)"
done
echo "ok"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment