Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a cron expression for every 2 weeks

Tags:

cron

Here is cron expression I tried 0 0 0 */14 * ? it is giving following schedules

Start Time:- Friday, September 8, 2017 1:25 AM

Next Scheduled :-

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 sept 29th, nxt schedule should be October 13 but it is scheduling for October 1

like image 694
Sat Avatar asked Sep 08 '17 05:09

Sat


People also ask

How do I run a cron job every week?

Show activity on this post. Position 1 for minutes, allowed values are 1-60 position 2 for hours, allowed values are 1-24 position 3 for day of month ,allowed values are 1-31 position 4 for month ,allowed values are 1-12 position 5 for day of week ,allowed values are 1-7 or and the day starts at Monday.

How do I run a cron job every 2 days?

As we already know that the pattern * */2 * * * executes the task for every 2 hour, similary can * * */2 * * execute the task for every 2 day (once in 2 days).

What is the use of * * * * * In cron?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .


3 Answers

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

cron for every 2weeks(on 1st and 15th of every month at 1:30AM) - 30 1 1,15 * *

like image 131
Sat Avatar answered Oct 09 '22 11:10

Sat


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

like image 38
amalesh Avatar answered Oct 09 '22 11:10

amalesh


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

like image 29
Karl Fecteau Avatar answered Oct 09 '22 11:10

Karl Fecteau