5

I want to start a recording job at a precise time. As I understand cron only guarantees a job to be started within a minute.

One possible solution is to start the cron job earlier and than wait until the precise time. What other possibilities do I have?

  • What accuracy are you looking for... I have a script which will time things to an accuracy of 0.01 of a second ... but of course that depends on the system load.... It uses date's nanosecond timer... Using seconds can at best give you an average of +/- 0.5 of a second... But it's unlikely that it matters... I've just put it together as an exercise... – Peter.O Feb 22 '11 at 00:01
  • I need 1/10 of a second. But the solution to start the cron job 2 minutes earlier and than wait e.g in a python script works fine. – Peter Hoffmann Feb 23 '11 at 19:18

1 Answers1

6

Easiest way is to put cron job to start it minute earlier and then add timing by your own.

You can also use at. At least man page of at says that you can use rather exact timing, including seconds. You can test this by adding at job that records time, for example shell script running

date > ~/at_test

If you go with cron route, date +%s is your friend.

For example

#!/bin/bash
target_time=1298301898
let wait_time=target_time-`date +%s`
if [ $wait_time -gt 0 ]; then
 sleep wait_time
fi
# Execute your code

With this approach your next problem is to figure out how to determine target_time. If you always know that you want to start it in the next minute, you can use

sleep 2 # Avoid running on xx:59, if cron starts immediately.
let wait_time=60-`date +%M`
sleep wait_time

to wait until minute changes.

Olli
  • 8,971
  • Using Olli's idea.. I've put together a script which will countdown, until a specified millisecond time.. It initially polls once per second (for the countdown display), until it is within a couple of seconds of the target time, and then shifts to checking th time every millisecond. Here is the link http://paste.ubuntu.com/570361/ ...(new link) – Peter.O Feb 22 '11 at 03:03
  • @fred.bear: That looks... complicated, but on the other hand when you need better accuracy than +-1s it's more difficult. – Olli Feb 22 '11 at 08:59