Doppelganger
Doppelganger

Reputation: 20835

How do I run a script on friday, once every two weeks?

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.

Upvotes: 2

Views: 3243

Answers (2)

kamituel
kamituel

Reputation: 35960

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).

Upvotes: 3

joel goldstick
joel goldstick

Reputation: 4493

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

Upvotes: 0

Related Questions