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

Viewed 75

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

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