LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Crontab entry to schedule a script every 14 days (https://www.linuxquestions.org/questions/linux-general-1/crontab-entry-to-schedule-a-script-every-14-days-725887/)

senrooy 05-14-2009 04:48 AM

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 ?

estabroo 05-14-2009 07:31 AM

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

druuna 05-14-2009 07:42 AM

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

colucix 05-14-2009 07:56 AM

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

druuna 05-14-2009 08:04 AM

@colucix: Of course!! Forgot all about that.

senrooy 05-20-2009 04:00 AM

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

druuna 05-20-2009 04:17 AM

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.

colucix 05-20-2009 05:30 AM

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
$(readlink -f $0)
EOF

the command readlink -f $0 gives the full path of the script itself.

michaelk 05-20-2009 05:38 AM

How about just:
0 5 */14 * * /your/command/

Should run every 14 days regardless of a specific date.

druuna 05-20-2009 05:40 AM

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

colucix 05-20-2009 06:15 AM

Quote:

Originally Posted by druuna (Post 3546803)
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.

You're right, druuna! Thank you for the clarification.

senrooy 05-26-2009 06:50 AM

Thanks a lot to you all.That command worked for me after some modifications.

sureshk.ganesan 06-03-2009 08:54 AM

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

jamtat 03-13-2019 03:09 PM

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
The word "yes" will appear after running this command if it is run during an even-numbered week.

If it is desired for the cron job to run in odd-numbered weeks, then
Code:

[ $(expr `date +%W` % 2) = 1 ] && echo yes
seems the right evaluation logic to use. Have I understood correctly how this works?

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
Further input on this, anyone?

rknichols 03-13-2019 03:39 PM

Quote:

Originally Posted by jamtat (Post 5973541)
Code:

[ $(expr `date +%W` % 2) = 0 ] && echo yes
The word "yes" will appear after running this command if it is run during an even-numbered week.

If it is desired for the cron job to run in odd-numbered weeks, then
Code:

[ $(expr `date +%W` % 2) = 1 ] && echo yes
seems the right evaluation logic to use.

Those will still glitch at the beginning of each year.

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
That computes the integer number of days since the epoch, modulo 14. That "0" could be any number {0..13}. (Yes, "date +%s" is a GNU extension.)


All times are GMT -5. The time now is 08:36 AM.