Crontab entry to schedule a script every 14 days
I need to schedule a script through crontab so that it runs every 14 days.
Can anyone tell me what should be the crontab entry for doing this ? |
not sure if there is a nice crontab way of doing it, but you could have it run every 7 days and your script could just quit every other time instead of doing work
|
Hi,
Something like this? 00 05 * * 1 [[ $(expr `date +%W` % 2) = 0 ]] && /path/to/script.sh This will run every week (1 -> first day of the week) at 05:00, checks if the week is odd or even. If it is even => run script. If not -> do nothing. Hope this helps. --- edit --- Take a look at colucix reply (#4), the % needs to be escaped!! Correct command should be: 00 05 * * 1 [[ $(expr `date +\%W` \% 2) = 0 ]] && /path/to/script.sh --- /edit --- |
druuna, you have to escape the % signs in the crontab entry, otherwise they are interpreted as newline characters and all data after the first % are sent to the command as standard input. This is a special behavior of crontab.
Code:
00 05 * * 1 [[ $(expr `date +\%W` \% 2) = 0 ]] && /path/to/script.sh |
@colucix: Of course!! Forgot all about that.
|
According to the requirement the schedule date should not be date specific i.e. on14th and 28th for example. It should run after every 14 days. I have tried with the above command. But it won't work as per requirement
|
Hi,
I guess I'm not sure I understand the requirements. The command given is not date specific, it runs every 7 days (and checks if the week is odd/even to make it every 14 days), in this specific example it always runs on the first day of the week (1 = monday). Without looking at my agenda I don't have a clue what mondays date is or the one that is 14(+) days ahead. After the fact you could make a schedule and get the dates it runs on, and you will get a nice stat that shows dates, 14 days apart. It seems to me that is what you want/asked for. Please elaborate on the requirements. |
In alternative you can try the at command, in place of crontab. To execute the same script after 14 days, try the following code at the end of your script:
Code:
at now + 14 days << EOF |
How about just:
0 5 */14 * * /your/command/ Should run every 14 days regardless of a specific date. |
@colucix: Nice, I love self-writing code :)
There is one possible downside to this: Even if the script runs for a short while (say minutes), this will create a sliding window; Every next occurence will run 14 days and a few minutes (maybe hours?) later then the previous one. Maybe you should place that code snippet in the beginning of the script instead of the end. The same 'problem' is still in effect, but we are talking about a second or so, which should keep you safe for a long while. |
Quote:
|
Thanks a lot to you all.That command worked for me after some modifications.
|
Hi Senrooy,
I too need the same thing which you have done. can you please post the script which you have done to schedule a job in crontab to run every 14 days. Thanks in advance |
Ok, I know this is a really old question but I'm trying to better understand the workings of the evaluation being done. Maybe rehashing it a bit will prove of use to someone searching for information on how to accomplish this sort of aim.
Some tests reveal that the evaluation logic will work for cron jobs that are supposed to run in even-numbered weeks (as calculated from the start of the year), as stipulated in one of the answers given and as the following seems to reveal: Code:
[ $(expr `date +%W` % 2) = 0 ] && echo yes If it is desired for the cron job to run in odd-numbered weeks, then Code:
[ $(expr `date +%W` % 2) = 1 ] && echo yes If so, a sample cron entry that should run some command every two weeks in odd-numbered weeks would look like this: Code:
00 05 * * 1 [[ $(expr `date +\%W` \% 2) = 1 ]] && /path/to/script.sh |
Quote:
If the goal is really "every 14 days" and not any particular day of the week, then Code:
[ $((($(date +%s)/86400) % 14)) = 0 ] && echo yes |
All times are GMT -5. The time now is 08:36 AM. |