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:I'm not aware of a builtin to so this, but the bash to use is something like:
EDIT: Added date and comment so you can track how long it has been failing for.
I would structure this in a bash loop. I suppose you could also poll the system (ping, nmap or otherwise) to determine the service state and connect if successful.
You can set the exit value to a non-zero value and then loop until the command returns with a exit 0 (success). Here is a script I have that does that:
Not the best of scripts - it was more of a thought experiment of ensuring that the rsync will restart when it fails until it has finished successfully.
Edit: In your case, you probably want something like:
You can add error conditions here and there, but the important thing is to immediately connect once you verify the port is open, which is what the && operation is for - it will execute the next command if the previous command was successful. (Until then, a failed connection will return a non-zero exit code, which will cause the loop to continue). Once you login successfully and then log out, the exit code that is returned from that successful login session will terminate the loop.
(the sleep command is optional, but note that in some cases, without a sleep loop, your display may slow down until you either minimize it or switch to the next window, which could be a problem if your display is a remote desktop)