Skip to content

Instantly share code, notes, and snippets.

@jsoref
Created September 2, 2022 22:28
Show Gist options
  • Save jsoref/58687138f26ab832562a20a6ec73c3d1 to your computer and use it in GitHub Desktop.
Save jsoref/58687138f26ab832562a20a6ec73c3d1 to your computer and use it in GitHub Desktop.
A simplistic way to ensure that a certain tcp host:port is available so that a service can depend on it
#!/bin/sh
#/usr/local/bin/wait-for-tcp-port.sh
instance="$1"
host="${instance%%:*}"
port="${instance##*:}"
start=10
loop=$start
while true
do
nc -zvw3 "$host" "$port" 2>/dev/null >/dev/null
result=$?
if [ $result = 0 ]; then
break
fi
if [ $loop = $start ]; then
printf "Sleeping " >&2
loop=$(( $loop - 1 ))
fi
printf "." >&2
sleep 1
done
echo >&2
#/etc/systemd/system/wait-for-tcp-port@.service
[Unit]
Description=Wait for '%i'
[Service]
TimeoutStartSec=infinity
ExecStartPre=/usr/local/bin/wait-for-tcp-port.sh "%i"
ExecStart=/bin/echo 'Found %i'
RemainAfterExit=yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment