Files
dotfiles/local/bin/x-when-down

50 lines
1009 B
Bash
Executable File

#!/usr/bin/env bash
#
# Wait until a given host is down (determined by ping) then execute the
# given command
#
# This script is based on the original work by Steve Kemp.
# Original work Copyright (c) 2013 by Steve Kemp.
#
# The code in the original repository may be modified and distributed under your choice of:
# * The Perl Artistic License (http://dev.perl.org/licenses/artistic.html) or
# * The GNU General Public License, version 2 or later (http://www.gnu.org/licenses/gpl2.txt).
#
# Modifications and enhancements by Ismo Vuorinen on 2025.
#
# Usage:
# ./when-down HOST COMMAND...
#
# Example
# ./when-down 1.2.3.4 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
wait_for_host_down()
{
local host=$1
echo "Waiting for $host to go down..."
while ping -c 1 -W 1 "$host" > /dev/null 2>&1; do
sleep 1
done
}
main()
{
local host=$1
shift
wait_for_host_down "$host"
"$@"
}
main "$@"