how to create a cron expression for every 2 weeks
Asked Answered
F

9

59

Here is a cron expression that I tried: 0 0 0 */14 * ?. It creates the following schedule:

Start Time:

Friday, September 8, 2017 1:25 AM

Next Times:

  1. Friday, September 15, 2017, 12:00 AM
  2. Friday, September 29, 2017, 12:00 AM
  3. Sunday, October 1, 2017, 12:00 AM
  4. Sunday, October 15, 2017, 12:00 AM
  5. Sunday, October 29, 2017, 12:00 AM

This expression is working for every 2 weeks in every month, but my requirement is it has to run for every 2 weeks. I mean after executing September 29th, the next date should be October 13, but it schedules for October 1.

Fardel answered 8/9, 2017 at 5:39 Comment(7)
I think you should save the last date you run your script and if current time it is greater than or equal to 14 days then it should run else it should die or exit.Erythrite
how to do these things, can you provide some technical info for this?Fardel
You need to create a file or store the date in database and then read that date and compare it with the current date using the following function ` public function date_differnece($d1, $d2) { $date1 = new DateTime($d1); $date2 = new DateTime($d2); return $date2->diff($date1)->format("%r%a"); }Erythrite
can't we define/create a direct Cron expression for every 2 weeksFardel
There must be some complex conditional way but not possible the way you are doing it. I am not that good in that so I suggested what I would have done.Erythrite
It's not possible. That's not how the Cron works. It's similar problem I responded here #45006420Attainture
this is not what your question asks, but another way to solve this is through a custom while loop that sleeps for 60*60*24*14 secondsElastic
F
69

There is no direct cron expression for every 2 weeks. I use the following cron expression, which is similar to 2 weeks, but not exactly for 2 weeks.

cron expression for every 2 weeks on the 1st and the 15th of every month at 1:30 AM:

30 1 1,15 * *
Fardel answered 15/9, 2017 at 6:45 Comment(2)
That's great, but why 1.30? I assume we can use any time even 00.00 right? 0 0 1,15 * * should work I guess.Punjabi
@SandeepChauhan it is typical to avoid using 0:00 to prevent for having multiple jobs running at the same time, so you normally add some random time to it, just in case (plus some countries have a daylight save time jumps at midnight). But you can do what you want, as you noted.Vivia
F
20

Friday every two weeks:

0 0 * * Fri [ $(expr $(date +%W) \% 2) -eq 1 ] && /path/to/command

Found on: https://cron.help/every-2-weeks-on-friday

Filipe answered 19/9, 2020 at 19:9 Comment(3)
It's possible to use 0 0 0 */14 * ? but not in combination with a specific day name because the support for specifying both a day-of-week AND a day-of-month parameter is not implemented.Filipe
@parisssss, this will run every 14 day of the month if its on friday.Bombsight
for me I have to fix it, escaping the first % too 0 0 * * Fri [ $(expr $(date +\%W) \% 2) -eq 1 ] && /path/to/commandOctad
W
7
30 7 1-7,14-21 * 1

“At 07:30 on every day-of-month from 1 through 7 and every day-of-month from 14 through 21 and on Monday.”

Wharve answered 10/5, 2021 at 10:27 Comment(2)
This runs approximately every two weeks but it is not perfect. For example, this expression would run on 2022-01-03, 2022-01-17, but would skip 2022-01-31.Conney
Another flaw is on the day number. Eg. In March 2022, the Mondays are on 7, 14, 21, 28. So it runs on the first 3 Mondays consecutively. The day could be '1-7,15-21' instead.Ratify
C
4

The crontab manual on my Ubuntu 18 says:

Note: The day of a command's execution can be specified by two fields — day of month, and day of week. If both fields are restricted (i.e., aren't *), the command will be run when either field matches the current time. For example, 30 4 1,15 * 5 would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday. One can, however, achieve the desired result by adding a test to the command (see the last example in EXAMPLE CRON FILE below).

and the mentioned example is:

# Run on every second Saturday of the month
0 4 8-14 * *    test $(date +\%u) -eq 6 && echo "2nd Saturday"
Colemancolemanite answered 27/5, 2021 at 15:48 Comment(0)
L
2

You need to specify a start day. Otherwise it's will always reset with the 1st day of the month. So this expression "0 0 0 23/14 OCT ? 2017" is every 2 weeks starting on October 23rd 2017

Logogriph answered 17/10, 2017 at 13:32 Comment(1)
This is actually "every 14 days starting fron 23rd, in October, in 2017". Therefore the only execution would be in 2017-09-23.Wingover
T
2

I found 0 */336 * * 1 to be a nice trick (every 14th day on Mondays).

(336 is the number of hours in 2 weeks - i.e. 24 x 14.)

Theatricals answered 3/4, 2023 at 18:26 Comment(1)
According to CronDrive this runs every Monday, not every other Monday.Coincidence
A
0

I got my 14 days cycle like this

# ----------- rsync backup job --------------
# this will execute the crontab if the week is even
50 17 * * 5 [ $(( $(date +\%V) \% 2 )) -eq 0 ] && /home/user/.backup/rsync_even.sh >> /home/user/.backup/rsync.log 2>&1
# this will execute the crontab if the week is odd
50 17 * * 5 [ $(( $(date +\%V) \% 2 )) -eq 1 ] && /home/user/.backup/rsync_odd.sh >> /home/user/.backup/rsync.log 2>&1
Amasa answered 28/4, 2023 at 16:0 Comment(0)
D
0
0 3 */2 * 4

This will run at 03:00 on every 2nd day-of-month if it's on Thursday. Ideally it runs every 2 weeks on Thursdays.

Using Thursday as an example, a week is an odd number(7days), every 2 days will not repeat a Thursday for 2 weeks (14 days). You will get an occurrence of a day if you run every 2nd day for 2 weeks.

Diachronic answered 17/5, 2023 at 10:3 Comment(1)
This simply won't work in most cron implementations. It runs on every second day and ADDITIONALLY every Thursday. It's a quirk of cron, yes, but it's how it is.Eucharis
S
0

0 0 1 2/14 * ? At 01:00 AM, every 14 days, starting on day 2 of the month

I specify the starting day of the month, this helps

Sulfide answered 19/5 at 17:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.