How to write cron expression to run a job that starts on Saturday and ends on Sunday? - cron

I'm pretty new to using crontab. I have a requirement to schedule an airflow DAG that should run every hour starting at 21:00 on every Saturday and run till 04:00 on Sunday.
i.e it should run according to the below schedule:
21:00 on Saturday(18th June 2022)
22:00 on Saturday(18th June 2022)
23:00 on Saturday(18th June 2022)
00:00 on Sunday(19th June 2022)
01:00 on Sunday(19th June 2022)
02:00 on Sunday(19th June 2022)
03:00 on Sunday(19th June 2022)
04:00 on Sunday(19th June 2022)
The next iteration should start on Saturday(25th June 2022) at 21:00
I tried using the below expression but it's showing invalid:
0 21-23,0-4 * * 6-0

You cannot have multiday spanning ranges set in single crontab entry because crontab file syntax is very limited and it simply does not support that. In your current form you would end up having job run also every Sunday 21:23 hrs too. The simplest workaround is to create two separate entries: first one for Saturday and 2nd one for Sunday, so there's no way of any overlap and ambiguition:
0 21-23 * * 6
0 00-04 * * 0

Airflow allow more flexible scheduling than just cron expression.
While Marcin answer is correct the context of the question is scheduling DAG on Airflow so I'll provide the solution for this case from Airflow side.
Since Airflow 2.2.0 a new feature was added: AIP-39 Richer scheduler_interval
A new concept of Timetable is available which allows you among another things to handle use cases that don't fit into a single cron expression. To do that you can follow Customizing DAG Scheduling with Timetables. In essence you register a Timetable via plugin that returns the values of when the DAG should be scheduled, so if you can write a Python function that can generate the scheduling logic Airflow can use this to schedule your DAG. You can see example for use function in this doc. In simple words if you can write a Python function that will return the next date according to your logic then you can use Timetable for the scheduling.

Related

Cron Scheduler - every quarter end third Sunday # 3:30 AM CST

Actually I am trying to schedule an application , which has to be run at
Cron expression to run application job for every end of quarter on 3rd Sunday at 3:30 AM CST
currently am using 0 */10 * ? * * - which runs for every ten minutes.
when I search on online ,this link https://crontab.guru/every-quarter
0 0 1 */3 * this would run for every quarter I guess.
But for my requirement , which I stated above, am not sure actually.
but , i referred some of the previous questions and some trial and error , I reached something like this 30 3 15-21 */3 SUN but am not sure. Please give your thoughts
OK .. for spring scheduler cron expression, below is my best bet
0 30 03 15-21 3,6,9,12 SUN
should work i think. tested partially. since, the above expression will run only on june.

Issue while running Monthly Cron Expression with Airflow

i need some help understanding the behaviour with monthly Cron expression [43 10 3,8,12 */3 *] with start_date as datetime(year=2019, month=11, day=18, hour=1, minute=30, second=0 , tzinfo=pendulum.timezone("UTC")) and end_date as None . This has backfill set as true .
Current Date is: 2020-10-19
As per my understanding it should not have triggered last two runs 10-03 and 10-08 . Can someone please help me understand this behavior? Also if it is triggering run for execution_date of 10-03 and 10-08 then why not for 10-12?
Could you elaborate on "it should not have trifggered the last two runs"?
The cron expression 43 10 3,8,12 */3 * matches:
“At 10:43 on day-of-month 3, 8, and 12 in every 3rd month.”
A good tool to validate cron expression is crontab.guru.
The execution date 10-12 hasn't triggered yet, because of how Airflow handles execution_date - see airflow scheduler:
The scheduler won’t trigger your tasks until the period it covers has ended e.g., A job with schedule_interval set as #daily runs after the day has ended. This technique makes sure that whatever data is required for that period is fully available before the dag is executed. In the UI, it appears as if Airflow is running your tasks a day late
Let’s Repeat That, the scheduler runs your job one schedule_interval AFTER the start date, at the END of the period.
This means the run with execution date 2020-10-12 10:43:00 will be triggered just shortly before 2021-01-03 10:43:00.

Cron Job Scheduling Different Dates of different month

Is there any way to schedule the CRON job for different days of different months!
For Example:
I need to schedule the job from February 25 to March 10 2017.this can be done by creating 2 jobs as
"0/1 * 25-28 2 2017 /cronjob.sh" and
"0/1 * 1-10 3 2017 /cronjob.sh"
But, cant I do it in one job? is there any way to do that!
No, you can't easily do this with just one cronjob.
You could, if you combine cron with some other scheduler, as in 0 * * * * otherScheduler && /cronjob.sh. But that's more complicated than simply two cronjobs.
You also could do this by changing the date on your server such that the cron schedule is from February 15 to 28. But that would be confusing and only useful if your server doesn't need to run anything else.

Quartz Cron expression with custom hours

I want to write a quartz cron expression that will fire my trigger in the following times:
7:40 AM, 1 PM, 6 PM.
I know that I can do this:
0 0,40 7,13,18 ? * 2-6 (to run it MON - FRI every day in the month)
But the problem is that it will actually run at 7:00, 7:40, 13:00, 13:40, 18:00, 18:30.
Please show me a best practice to such issue.
Thanks,
Steve

how to build cron expression for hours and minutes

i want a cron expression for a schedule which runs for every 2hr 10 min and output am expecting is
2:00
4:10
6:20
i tried 0 0/2 0/2 * * ? for that and the output was like this
Thursday, November 1, 2012 12:50 PM
Thursday, November 1, 2012 12:52 PM
Thursday, November 1, 2012 12:54 PM
source-http://www.cronmaker.com
Thanks in advance..
I too had searched a lot previous to run a cron for every (> 60 ) minutes, but was not able to find any solution.
Best way to implement the solution to your problem, is to write your own script that would check the script's last run which you can handle in any way (check timestamp, log the last run somewhere, etc) and would run the required job if only the time conditions are met.
Then put a cron to call this wrapper script every 10 mins (in your case), as this would ensure, it would get checked for each of the time you would have expected the original final job to run.
Hope this helps.

Resources