2

I could use cron, but I can't figure out if there's a way to set the right schedule. I can also check the date in Python, running the script via cron everyday, but checking the right date inside my (Python) script (which I assume has more powerful conditions).

I thought on limiting one run on fridays between 1 and 7, and the other one on fridays between 15 and 21. But this option would have a problem on months like 3/2013 which have 5 fridays.

Doppelganger
  • 20,114
  • 8
  • 31
  • 29

2 Answers2

3

Is this what you're looking for?

Put this in crontab:

0 7 * * 5 sh -c " if [ $(expr $(expr $(date +\%s) \/ 604800) \% 2) -eq 0 ]; then command; fi "

This would run you command every other Friday at 7.00 AM.

Note: number 604800 means one week (3600sec * 24 * 7).

kamituel
  • 34,606
  • 6
  • 81
  • 98
  • That doesn't work for me: "/tmp/crontab.XXXX779ZQ1":1: bad day-of-week – Doppelganger Mar 01 '13 at 13:49
  • The answer here explains why that shouldn't work: http://stackoverflow.com/questions/9687278/cron-job-run-every-x-weeks-and-on-specific-days – Doppelganger Mar 01 '13 at 13:54
  • 1
    You're right. Sorry for not checking it before. I've corrected the answer, now it's at least syntactically correct and I think it'll work. – kamituel Mar 01 '13 at 14:14
  • My guess is that this solution has a problem on the last week of the year. Sometimes the last Friday of the year is on week 51, and some other times on week 52. So that could make you run the script two weeks in a row or once in three weeks – Doppelganger Mar 01 '13 at 14:18
  • Hah, you keep me challenged ;) I suppose now the solution is free from the edge case of the new year? (I'm counting number of weeks from unix epoch, not from Jan, 1st. – kamituel Mar 01 '13 at 14:29
0

Why not run the cron job each friday, but add code to write the last date ran in a file. Check to see if two weeks has passed, rewrite the file, and run the rest of the cron job

joel goldstick
  • 4,393
  • 6
  • 30
  • 46