Files
dotfiles/local/bin/x-when-up
Ismo Vuorinen bb3f4a8f6c Many updates and improvements
- yamllint
- shfmt config
- fix Go bin path
- fix git credentials config
- add nvm default packages
- updated Brewfile
2023-03-28 15:27:44 +03:00

46 lines
839 B
Bash
Executable File

#!/bin/sh
#
# Wait until a given host is online (determined by ping) then execute the
# given command
#
# Usage:
# ./when-up HOST COMMAND...
#
# Example
# ./when-up 1.2.3.4 ssh 1.2.3.4
#
# Special case:
# when using when-up to ssh to a host, this host does not need to be given twice
# ./when-up ssh 1.2.3.4
#
#
# Ensure we received the correct number of arguments.
#
if [ $# -lt 2 ]; then
echo "Usage: $0 HOST COMMAND..."
exit 1
fi
if [ "$1" = "ssh" ]; then
HOST=$2
else
HOST=$1
fi
echo "Waiting for $HOST to come online..."
ping -c 1 -W 1 "$HOST" > /dev/null
while [ $? -ne 0 ]; do
sleep 1
ping -c 1 -W 1 "$HOST" > /dev/null
done
# By the time we reach here the ping-command has completed successfully
# so we can launch the command we were given - along with any arguments.
if [ "$1" != "ssh" ]; then
shift
fi
"$@"