Skip to content

Instantly share code, notes, and snippets.

@ryran
Last active December 19, 2015 15:19
Show Gist options
  • Save ryran/5975549 to your computer and use it in GitHub Desktop.
Save ryran/5975549 to your computer and use it in GitHub Desktop.
A simple bash function for retrying commands until they succeed.
#!/bin/bash
# 'Loop' command
# I use this mostly for when machines are booting up and I don't want to
# keep manually trying to ssh over to them
function L {
local interval
if [[ $# -eq 0 || $1 = --help || $1 = -h || $1 = -\? ]]; then
echo "Usage: L [-N] COMMAND"
echo "Runs COMMAND [waiting N sec between failed attempts] until it succeeds"
echo "If wait interval N is not specified, it defaults to 2 sec"
else
[[ ${1#-} -gt 0 ]] && { interval=${1#-}; shift; } || interval=2
until $@; do
sleep $interval
done
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment