Run a Command Until it Stops Failing
I want to run a command until it completes sucessfully, then stop. I know I can set up a bash loop to do this, but I'm curious if there are any portable ways to do this similar to watch -n5
that would do what I'm looking for.
The use case: I'm trying to connect via SSH to a server that may not be up yet.
Just wrap said bash loop in a script, using $1
to specify the actual command to be executed and $2
for an arbitrary sleep value. Doing this, you'd be able to run the command like:
$ reallyrunit "ls" 5
# xxx.xxx.xxx.xxx = ipaddr ssh host until [ `nmap --open -p 22 xxx.xxx.xxx.xxx |grep -c "ssh"` -eq 1 ] do sleep 4 done # ssh stuff here
I'm not aware of a builtin to so this, but the bash to use is something like:
RET=1;until [[ $RET = 0 ]];do echo "$(date): About to make an attempt to connect"; ssh 192.168.33.12; RET="$?";sleep 1;done
EDIT: Added date and comment so you can track how long it has been failing for.