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?

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
  • 9,129