Silly trick

I’m running a script to do something a friend of mine asked for. (If you care about the details, go here.) We’re going out to a birthday thing with Tina’s family in about half an hour, and there’s a good chance it’ll run longer than that. (I actually have a pretty good idea of how long it’ll run, but let’s ignore that for now.)

So I was going to set up a loop to upload the results to somewhere every 15 minutes or so. But I’ll be out until later today, and it’ll be uploading the same thing every 15 minutes until I get home and stop it. (The bandwidth involved isn’t very big, but let’s pretend for a moment that this is a consideration.) But I want him to be able to check the progress, so making the interval much longer is potentially annoying. I could work out when the process has finished and stop uploading, but let’s assume that I don’t know how to do that off the top of my head (because I don’t) and that I don’t have time to work it out (which I probably do, but I’m blogging about it instead).

Then an idea popped into my head out of nowhere: Double the interval every time.

This way, it’ll be updating regularly at the start, so he (and I) can check the progress almost in realtime. Then the rate will slow down exponentially so that it’s not uploading all the time unnecessarily. And the delay between when the process finishes and when it does the final upload will naturally be in the order of the duration of the process itself (and in fact will be strictly less than it). And there will be very few extraneous uploads after it’s finished until I get home and kill it.

For this particular problem it was probably overkill, but if you change the scale of the problem and remove an easy way to find out when the process has ended or how long it will take in advance, it might actually be useful. It even seems to approximate people’s habits (or at least, my habits) when checking the progress of a long-running process – I tend to watch it closely for a minute or so, then check it occasionally at increasing intervals until it’s done.

Plus it was dead simple to do:

n=1; while true; do {upload stuff}; sleep ${n}s; n=$(($n*2)); done

Is this a known trick? Or does it even make sense?

Update: Looks like it’s going to finish just in the nick of time. Still… cool trick, right? Right?

No comments

No comments yet. Be the first.

Leave a reply