Created
September 2, 2022 22:28
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/etc/systemd/system/[email protected] | |
[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