summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Bobov <anton@bobov.name>2025-02-07 00:17:49 +0500
committerAnton Bobov <anton@bobov.name>2025-02-07 00:20:10 +0500
commit66ac4625bca22a6423ce24c82c45fced0899f3df (patch)
tree09bea132508cb282a78cec8666dd2691d7d0b3a6
parent7e6f1da0c797b26d587071a07702c68873b6736d (diff)
Add script to wait for port availability
-rwxr-xr-xport-wait52
1 files changed, 52 insertions, 0 deletions
diff --git a/port-wait b/port-wait
new file mode 100755
index 0000000..7f12d27
--- /dev/null
+++ b/port-wait
@@ -0,0 +1,52 @@
+#!/usr/bin/env bash
+
+set -euo pipefail
+
+help() {
+ cat <<EOF
+Usage: ${0##*/} [-p] [-h] [HOST] PORT
+
+Wait until specified PORT is available on HOST (localhost if HOST ommited).
+
+ -p print progress every second
+ -h print this help
+EOF
+}
+
+main() {
+ show_progress=""
+ while getopts "ph" opt; do
+ case "${opt}" in
+ p)
+ show_progress=t
+ ;;
+ h)
+ help
+ exit
+ ;;
+ *)
+ help
+ exit 1
+ ;;
+ esac
+ done
+ shift $((OPTIND - 1))
+
+ if [ $# -eq 2 ]; then
+ host="$1"
+ port="$2"
+ elif [ $# -eq 1 ]; then
+ port="$1"
+ else
+ echo "No host/port provided"
+ help
+ exit 1
+ fi
+
+ until nc -z "${host:-localhost}" "$port"; do
+ [ -n "$show_progress" ] && echo -n .
+ sleep 1
+ done
+}
+
+main "$@"