sleep is my favorite command, no doubt about it, but there was a couple of times that I needed to run a command at specific time.
I believe, ideally, you should use at command. But I never have installed at package on my Gentoo ever, well, I don’t think I have ever used it, actually. Your Linux distribution may still have it installed by default.
An alternate way, cron seems to be fine, but I think that’s a misuse of cron. From what I understand, the purpose of cron is for running task at regular basis, not just a one-time deal. Besides, it takes some little effort to edit crontab, not really worth it.
So, I ended up doing like:
sleep 3h17m && run-the-command
I manually calculated the sleep time. Not really precise, but it’s fine with me, I didn’t need to run it at exactly second.
I had even written something like (quick example, not actual code):
while [[ $(date +%H%M) == 1723 ]]; then ... ; sleep 1 ; done
I have to say, this way definitely was not the best way. It’s a one-liner, sure, but too long and not very flexible.
Now, my solution to this is much better, sleeptil function:
$ sleeptil -v 09:23:22
Wed Nov 7 09:23:22 CST 2012 in 40 minutes 29 seconds
$ sleeptil -v 07:23:22
Wed Nov 7 07:23:22 CST 2012 is in the past!
$ sleeptil -v tomorrow 07:23:22 UTC
Thu Nov 8 15:23:22 CST 2012 in 1 day 6 hours 40 minutes 10 seconds
$ sleeptil -v 12/25 CET
Tue Dec 25 07:00:00 CST 2012 in 47 days 22 hours 15 minutes 37 seconds
You pass a date/time string, the same you use in date -d '...'. It shows you the parsed time and how long will it sleep. If you may want to read more about info date, especially about relative terms.
-v means verbose. If not specified, then sleeptil runs like sleep, it won’t print out anything except errors.
You can use relative terms or a timezone to specific a time, there would be no headache if you don’t know how to convert time between timezones for your next online meeting. You get the local time and an ETA. The ETA you read above is human readable, via my td.sh library. If td.sh can’t be found, sleeptil simply prints out the ETA in seconds, eg. “… in 239288 seconds.”
Well, even this can be used for a time like ten years later, despite the sleep accuracy, it shouldn’t be since it require a constantly running process. For that, you should use a calendar program. Anyway, I only intend to use this function for time within 24 hours or as further as my computer would be on.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.