Re-running a Unix command, until it completes successfully – e.g.: imapsync

I’m doing an email migration for a client from an old 2008 SBS Server into Office 365. For some reason, there were two mailboxes that just wouldn’t migrate using the migration wizard in Office 365.

I switched to the ever-trusty imapsync which I’ve used to migrate more mailboxes than I care to remember.

As an aside, I had a few issues with imapsync from MacPorts so ended up downloading a fork from GitHub that resolved the issue, however I had to install a few CPAN modules for Perl manually. I’ve lost the link to the GitHib version, however it was easy to find initially by searching on the error string that it was returning when trying to run it – something about an SSL error.

Anyway, after building and installing everything required, imapsync kept erroring out on these two mailboxes after some random number of emails migrated. After logging in and restarting it manually a few times, I thought that there had to be a better way.

Looking further into the issues, imapsync was exiting with a return code of 2, indicating that an error occurred. When it completes successfully, it should exit with a return code of 0. This makes it easy to just keep running it until it exits with zero;

until imapsync --option1 --option2 ... --optionn; do
    echo Exited with an error, rerunning...
done

Nice and easy…

The until loop keeps running the command given to until (often a check for something == 0) exits with a true (or zero) exit status. The echo statement gets executed as part of the until loop, however this is more a side effect of running imapsync as the until loop predicate.

 

This article was posted by Kai Howells. If you liked this content and have any technical work in the Melbourne area, say hello via my contact form or give me a call on 0419 361 653 - I cover most of the greater Melbourne area and my rates are competitive.

Leave a Reply

Your email address will not be published. Required fields are marked *