I would like to create a loop that repeats a ncftp transfer if it returns an error.
I'm a little unsure how the exit code variable can be used in a loop. Would something like this work?
until [$? == 0]; do
ncftpput -DD -z -u user -p password remoteserver /remote/dir /local/file
done
I found the basis for this elegant loop elsewhere on serverfault. Turns out there is no need save the exit code, as you can test directly on the command itself;
Almost. You are probably better saving the return value as a variable so that you can pre-set it before the loop. Otherwise it will be affected by the last-run command.
You might also want to sling a
sleep
in there to stop it respawning too quickly.Bit hacky but my solution was to just create a bash function which runs itself if it exits with a failure
Some of the solutions shown on this page will loop an infinite number of times if the command continues to fail.
If you're not a fan of infinite loops try this:
Now, whenever you need a retry loop, just run:
e.g.:
You can do a loop while your command returns error:
I've had a similar problem with needing to repeat curl requests, and the scripting was getting out of hand. As a result I created the retry tool:
In this case, the ncftpput command will be retried indefinitely until it returns a success status code, backing off by default for ten seconds in between.
Retry is available here: https://github.com/minfrin/retry