Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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 0: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
up vote 5 down vote accepted

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

let wait_time=target_time-`date +%s`
if [ $wait_time -gt 0 ]; then
 sleep wait_time
# 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.

share|improve this answer
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 ...*(new link)* – Peter.O Feb 22 '11 at 3: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 8:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.