Hi babinu and welcome to LinuxQuestions!
Quote:
Originally Posted by babinu
I would like add a cron for running a job every 2 saturdays .....
How do you do that?
|
Again you cannot specify this using the crontab time specifications, hence in my opinion you are forced to put a check at the beginning of your script and embed all the code as a block inside a control statement.
Said that, you can simply run your job every Saturday:
Code:
30 9 * * 6 /path/to/script.sh
and decide whether to run on odd or even weeks of the year. Using the date command the script can retrieve the number of week of the year, then check the remainder of the division by two. In bash you can put it in a one-liner using arithmetic substitution with a nested command substitution, like this:
Code:
if [ $(($(date +%W) % 2)) -eq 1 ]
then
echo "Odd week! Running..."
<block of code here>
else
echo "Even week! Do nothing..."
fi
Substitute 1 with 0 in the test expression if you want to run every even week.
A downside is on year changes. Last week is 53 and first week of the new year is odd as well, so that your job will run two consecutive weeks. To avoid that you can:
1. Add a more fined control on even or odd years and switch to even or odds weeks accordingly.
2. Use a totally different approach and create/delete a dummy file every time the script runs. It will check for the presence of the file and a) if the file is absent don't run and create it, b) if the file is present run and delete it. In other words you can use a file as a toggle (for example you can create/delete a hidden file in your home directory with a unique name).
Edit: I checked and on my system the last week of the year is 52, whereas the first week is 00. Anyway, this does not change the considerations above.