Skip to content

Instantly share code, notes, and snippets.

@dermoumi
Last active October 16, 2017 10:27
Show Gist options
  • Save dermoumi/13715ee4f0f4c25edad2f5f61983b5bc to your computer and use it in GitHub Desktop.
Save dermoumi/13715ee4f0f4c25edad2f5f61983b5bc to your computer and use it in GitHub Desktop.
#!/bin/bash
# Small utility to wait for port to open.
#
# usage:
# ./port-wait.sh hostname port [timeout=10]
#
# ex:
# ./port-wait.sh 127.0.0.1 3000
host=$1
port=$2
if [ -z "$host" ] || [ -z "$port" ]; then
echo "usage: ./$0 hostname port [timeout=10]"
exit 1
fi
timeout=$3
if [ -z "$timeout" ]; then
timeout=10
fi
end_date=$(($(date +%s) + $timeout))
until nc -z $host $port; do
sleep 1
if [ "$end_date" -lt "$(date +%s)" ]; then
exit 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment