Run Linux script at time 'n' on first day, 'n+15 mins' next day, and so on

5

1

I'm trying to take timelapse images using my webcam. What I want to do is run a script at midnight on 1st Jan, then 00:15 on 2nd Jan, 00:30, 3rd Jan and so on. This can keep running forever, I'll switch it off when I'm done.

I can't do this with Cron, can I, without filling a crontab with every possible combination? I don't know what else to do though.

Piku

Posted 2009-12-16T20:40:27.343

Reputation: 557

That will make for an interesting time lapse. Can you post a link when it's done? – marcusw – 2009-12-16T21:03:37.733

If I make it work, and I remember then yeah. – Piku – 2009-12-16T21:42:11.977

Answers

7

Instead of creating a new cron job in your script, and having to remove the "old" cron job every time too, you should use the at command instead. The at command was created exactly for that, to let a command run once at a specific date/time. This way, you won't have to remove the old cron job.

So, you could have your script, lets call it "capture_from_webcam.sh", looking like that :

#!/bin/bash

#schedule next capture for tomorrow + 15 minutes
echo "/path/to/capture_from_webcam.sh" | at tomorrow + 15 minutes

# capture from webcam
/path/to/capture_from_webcam.sh

And to have if execute for the 1st time, at midnight on January 1st :

echo "/path/to/capture_from_webcam.sh" | at 00:00 01/01/2010

For more information, see the at man page.

Laurent Parenteau

Posted 2009-12-16T20:40:27.343

Reputation: 648

That's a nice one :-) What happens if the server is rebooted though? – ℝaphink – 2009-12-16T23:05:20.343

1@Raphink at jobs are handled by the cron deamon. They are store inside /var/spool/cron/atjobs. So, if nothing purge that directory on reboot, they will still be executed. – Laurent Parenteau – 2009-12-16T23:31:14.517

3

You can create a cron job as part of the script you're planning on running. So have the cron job run once, then the bash script creates the next one 15 minutes later and so on.

Here's a previous question that may help: How to create cron job using bash

Zurahn

Posted 2009-12-16T20:40:27.343

Reputation: 782

Cunning, I like that :) – Piku – 2009-12-16T21:00:37.113

Just be careful to make sure the script deletes the job that has just run, or you'll end up with hundreds of cron jobs which will be run again next year... – marcusw – 2009-12-16T21:02:54.940

1

You can do this via cron.

You call the same cron script every day at the same time, but you add a sleep statement to the start of the script that sleeps for 15m * date +'%j', this day of the year, [ 001 ... 366 ].

Darren Hall

Posted 2009-12-16T20:40:27.343

Reputation: 6 354

Hmm need to add modulus for 96 to prevent an overflow. – Darren Hall – 2009-12-16T20:50:52.250