Skip to content

Instantly share code, notes, and snippets.

@NothingCtrl
Created May 30, 2023 07:24
Show Gist options
  • Save NothingCtrl/80db8a163e26fb89e62f1a94c5ff638d to your computer and use it in GitHub Desktop.
Save NothingCtrl/80db8a163e26fb89e62f1a94c5ff638d to your computer and use it in GitHub Desktop.
Delay command execute until remote host available
#!/bin/bash
REMOTE_HOST=$1
REMOTE_PORT=$2
TIMEOUT=1
shift 2
cmd="$@"
if [ -n "$REMOTE_HOST" ] && [ -n "$REMOTE_PORT" ]; then
while ! nc -w $TIMEOUT -z $REMOTE_HOST $REMOTE_PORT; do
>&2 echo "Wait for remote host $REMOTE_HOST:$REMOTE_PORT"
sleep 1
done
>&2 echo "Remote host $REMOTE_HOST:$REMOTE_PORT connect OK"
fi
# run the command
exec $cmd
@NothingCtrl
Copy link
Author

NothingCtrl commented May 30, 2023

Usage example: /bin/bash wait-for-remote-host.sh 127.0.0.1 3377 echo Hello

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment