The New Utovsky Bolshevik Show

Tue, 04 Nov 2008

Running cron fortnightly: round-up

It's a few days on from my original post, so I thought I'd give a summary of people's ideas. Just to remind you of the aim of the exercise, it's to get a cron job to run every fortnight, without using the 'date' command.

So, here are the ideas we've had:

Well, in light of the above ideas, my solution might be slightly cheaty. It depends on a Debian (or, presumably, Ubuntu system), as it takes advantage of /etc/cron.weekly. Essentially you write a script that live in /etc/cron.weekly and symlinks other scripts in and out of /etc/cron.weekly. These scripts could be stored in, for example, /etc/cron.fortnightly. It would scan through there and symlink in any that weren't in /etc/cron.weekly and remove existing symlinks to /etc/cron.fortnightly scripts.

This has the advantages of:

Of course, we should all just use 'date'...

EDIT: benji has noted that /etc/cron.weekly is by no means a Debian-specific feature. SUSE has it. Warn your distributor now.

Posted: Tue, 04 Nov 2008 21:56 | Tags: , , | Comments: 4 |


0 0 */14 * *
Runs at midnight every 14 days.

man cron

Or you can try the sandbox here:
(i used it to verify the above as a sanity check).
Posted by liam christopher at Tue Nov 11 20:46:20 2008

liam, that's essentially the 1st and 15th solution, it's not actually fortnightly.

For example, your sandbox has:
  Mon  29 Dec  2008  0000
  Thu  01 Jan  2009  0000

which are obviously not 14 days apart.
Posted by Daniel Watkins at Tue Nov 11 22:09:51 2008

Duh. -1 to me for not looking at my own link very closely. I have actually hit a similar problem before with Date::Manip recurrences. The problem with "every two weeks" is the same as the relative dates "two days after", which is: after what?

To do a proper date recurrence, you need an anchoring point, but cron does not keep a state like that. Cron schedules absolutely against the clock, which does not have a notion of an anchor. However, if they had just put in week of year, instead of day of week, a */2 would make sense in that context.

So yeah, there are lots of ways to do it, and from the context of a shell, it probably should be date.

Though I would suggest using:
`date  "%W"` % 2
%W is week of year. Your other solutions are novel, but indeed insane.
Posted by liamchristopher at Fri Nov 14 04:11:13 2008

Even that solution isn't quite right.  It won't maintain fortnightly-ness when a year ends, because "365 % 7 != 0".  The only absolute time that `date` will give is seconds since epoch, so that's what you have to use.
Posted by Daniel Watkins at Fri Nov 14 11:46:04 2008