Crontab job on sunday with two week interval

Hi experts,

Please help me to set a backup script run at 6AM on Sunday every two week
It means
the 1st sunday, run script backup at 6AM
the 2nd sunday, skip
the 3rd sunday, run script backup at 6AM
the 4nd sunday, skip
... so on

Thank you so much

I don't think that this is possible by only crons means (at least not to my knowledge). What you could do is put something into the script which makes it run the "action"-part only every other run:

#! /bin/ksh

if [ -e /some/file ] ; then
     rm -f /some/file
else
     touch /some/file
     exit 0
fi

... do here what your script is supposed to do ....

exit 0

On the first run the script will not find /some/file, so it will create it and exit. On the second run it will remove it and then proceed to do whatever it is supposed to do. On the next run it won't find the file (because it removed it) and therefore will only create it and then exit, ....

This script can be used weekly in cron but will do its intended purpose only every other week.

I hope this helps.

bakunin

Or you could just map out what date every other sunday will be for the rest of the year and put those dates in the cronjob. Then do the same thing at the end of the year.

You'd think that the crontab line would be something like this:

[minute] [hour] 1-7,15-24 * 0 [function]

I.E. You're saying run the function at the time specified every Sunday of every month, when the date is 1st thru 7th or 15th thru 24th.

This creates two problems:

A - Because of a quirk in cron, the job will actually run EVERY Sunday AS WELL AS every day from the 1st to the 7th and 15th to the 24th.
B - If that quirk did not exist, you'll miss a week when there are 5 Sundays in the month (this may or may not be a problem for you),

Simple solution:

Have cron run it EVERY Sunday. Then have your script either check the current date, assuming you want to skip the 5th Sunday of the month), or check and set a flag in a file to indicate if it ran that week or not.

Note: This second idea is essentially what Bakunin said.

Thank bakunin and other experts
Your ideas is excellent, i set it already
Regards