Created
May 30, 2023 07:24
-
-
Save NothingCtrl/80db8a163e26fb89e62f1a94c5ff638d to your computer and use it in GitHub Desktop.
Delay command execute until remote host available
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/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example:
/bin/bash wait-for-remote-host.sh 127.0.0.1 3377 echo Hello