Thread: slocate
View Single Post
Benson's Avatar
Posts: 4,930 | Thanked: 2,272 times | Joined on Oct 2007
#6
Albright, that'd work for a while, but eventually the execution time of the rebuild makes drift out of sync, and then you could go weeks without an update.

A better thing is to check once an hour or so, so (assuming your plug-in at night is precisely periodic), it'd drift an hour, then get caught on the "previous" iteration. But then you do have to check when it was last run so you aren't running it 6 times a night. I assume that's what you were trying to dodge.

Try:
Code:
#!/bin/sh
while :; do
  if [ `date +%H` == 00 ] && sleep 10 && [ "x`battery-status|grep Charging`" != x ]; then
    play-sound /usr/share/sounds/ui-wake_up_tune.wav
  fi
  sleep 3590
done
The naive approach (sleep 3600) would skip once in a great while, because even when nothing happens, the condition evaluation and loop overhead cause drift. Every once in a while, it'll be 23:59:59.99 one iteration, and 01:00:00.01 the next, so you'll miss one. We avoid that by fudging the time below an hour, but delaying extra when we do hit so we don't hit it twice in the same hour. You could shift the sleep 10 into the then clause, but then it would sometimes check at 00:00:01, see the battery's not charging, and then check again at 00:59:51 and (if it was plugged in then) then run it. Your choice which behavior is better.



Rick, your
Originally Posted by rcull View Post
locate
Code:
grep "$1" /media/mmc2/locatedb | more
would work, but if you intend to call your script locate, it'd be good to behave a little more like locate. Leave out the '| more', so it could be used in scripts/pipelines.
 

The Following User Says Thank You to Benson For This Useful Post: