Sub topic 5
Scheduling a job with cron and time zones
This chapter assumes you can create a multi-task job, as covered in earlier chapters, and that you have seen cron-style schedules before. It covers the Quartz cron syntax that Databricks uses, how time zones and daylight saving time affect run times, and how to change a schedule through the Jobs API.
How a schedule fits into a job
A schedule is a job-level setting. When the time matches, Databricks creates a new run of the whole job, and the tasks then follow their dependency graph. The schedule has three fields:
quartz_cron_expression: when to fire.timezone_id: the time zone in which the expression is read.pause_status:UNPAUSEDorPAUSED.
A paused schedule keeps its definition but creates no runs. You can still start the job manually with Run now. A job can have a schedule or a trigger (see the next chapter), not both.
Quartz cron syntax
Databricks uses Quartz cron expressions, not the five-field Unix cron format. A Quartz expression has six required fields and one optional field:
| Position | Field | Allowed values |
|---|---|---|
| 1 | Seconds | 0-59 |
| 2 | Minutes | 0-59 |
| 3 | Hours | 0-23 |
| 4 | Day of month | 1-31 |
| 5 | Month | 1-12 or JAN-DEC |
| 6 | Day of week | 1-7 (1 is Sunday) or SUN-SAT |
| 7 | Year (optional) | 1970-2099 |
The special characters are:
*means every value.,separates a list, such asMON,WED,FRI.-is a range, such asMON-FRI./is a step, such as0/15for every 15 units starting at 0.?means "no specific value". It is allowed only in the day-of-month and day-of-week fields.Lmeans "last", such asLin day of month for the last day of the month.Wmeans the nearest weekday, such as15W.#means the nth weekday, such as6#3for the third Friday.
Quartz requires that exactly one of day of month and day of week be ?. You cannot constrain both. If you write 0 0 6 * * *, validation fails. Write 0 0 6 * * ? instead.
Some common schedules:
| Expression | Meaning |
|---|---|
| `0 0 6 * * ?` | Every day at 06:00 |
| `0 0/30 * * * ?` | Every 30 minutes, on the hour and half hour |
| `0 15 2 ? * MON-FRI` | Weekdays at 02:15 |
| `0 0 8 1 * ?` | The first day of each month at 08:00 |
| `0 0 23 L * ?` | The last day of each month at 23:00 |
| `0 0 9 ? * 6#1` | The first Friday of each month at 09:00 |
Databricks enforces a minimum interval of 10 seconds between runs triggered by a schedule, whatever the seconds field says. Very frequent schedules are rarely a good fit for jobs anyway. Starting a cluster takes minutes, so consider a streaming job or a trigger instead.
The schedule in the job definition
This fragment is the schedule block from the daily pipeline definition used earlier in this tutorial:
{
"schedule": {
"quartz_cron_expression": "0 0 6 * * ?",
"timezone_id": "UTC",
"pause_status": "UNPAUSED"
}
}Read it field by field: seconds 0, minutes 0, hour 6, any day of month, any month, and no specific day of week. The job runs every day at 06:00 UTC.
Time zones and daylight saving time
timezone_id takes a Java time zone ID such as UTC, America/New_York or Europe/Berlin. Use region IDs, not abbreviations like EST, because an abbreviation does not follow daylight saving rules.
The same cron expression fires at different UTC instants depending on the zone. 0 0 6 * * ? in America/New_York is 11:00 UTC in winter and 10:00 UTC in summer. If a downstream consumer expects data at a fixed UTC time, that shift can surprise it twice a year.
Daylight saving transitions also cause edge cases:
- When clocks move forward, the skipped local hour does not exist. A job scheduled in that hour may not run at its usual time on that day.
- When clocks move back, a local hour occurs twice. Frequent schedules, such as hourly ones, can run more often than you expect during that period.
Databricks documents this behavior for zones that observe daylight saving time. To avoid it, schedule in UTC. If the business really needs local time, such as "6 AM in New York", accept the shift and design tasks to be safe when run twice or late.
What "scheduled time" means
The cron time is when Databricks creates the run, not when your code starts. Three delays follow:
- Cluster start-up. A job cluster must be provisioned before the first task runs. This often takes several minutes.
- Queueing, if you enabled it on the job.
- Task retries, which push back later tasks.
If a report must be ready by 07:00, schedule the job earlier than 06:55 and leave margin. Use a timeout (covered later in the retries chapter) to catch runs that overshoot.
Interaction with concurrent runs
If a scheduled time arrives while the job already has max_concurrent_runs active runs, that scheduled run is skipped. Skipped runs are not queued and not retried later unless queueing is turned on. A daily job whose run takes 25 hours will therefore miss the next day's run when max_concurrent_runs is 1. The earlier definition used that setting on purpose, because overlapping runs of an incremental pipeline can write the same data twice.
Changing a schedule with the Jobs API
You can update only the schedule with jobs/update. The new_settings object replaces the top-level fields you pass, so include the complete schedule object. This script moves a job to weekdays at 07:30 in Berlin time. It reads the host, token and job ID from environment variables:
import os
import requests
host = os.environ["DATABRICKS_HOST"].rstrip("/")
token = os.environ["DATABRICKS_TOKEN"]
job_id = int(os.environ["JOB_ID"])
payload = {
"job_id": job_id,
"new_settings": {
"schedule": {
"quartz_cron_expression": "0 30 7 ? * MON-FRI",
"timezone_id": "Europe/Berlin",
"pause_status": "UNPAUSED",
}
},
}
resp = requests.post(
f"{host}/api/2.1/jobs/update",
headers={"Authorization": f"Bearer {token}"},
json=payload,
timeout=30,
)
resp.raise_for_status()
print("Schedule updated for job", job_id)To pause without losing the definition, send the same schedule with "pause_status": "PAUSED".
Common errors
- Both day fields set. Using
*in both day-of-month and day-of-week is rejected. Make one of them?. - Five-field Unix cron.
0 6 * * *has too few fields. Add the seconds field in front and change one day field to?. - Off-by-one weekday numbers. In Quartz, 1 is Sunday. Prefer names such as
MON-FRI. - Abbreviated zones. Use
America/New_York, notEST. - Forgotten pause. A copied job keeps
UNPAUSEDand starts firing. SetPAUSEDin the copy until you verify it. - Expecting catch-up. A schedule that was paused or skipped does not replay missed times. Backfill with parameters or a manual run.
Exercise
Write a Quartz cron expression and timezone_id that run a job at 02:15 every weekday (Monday to Friday) in New York local time. Then name one risk of that choice compared with using UTC.
Show answer
Use "quartzcronexpression": "0 15 2 ? * MON-FRI" with "timezoneid": "America/NewYork". The day-of-month field is "?" because the day of week is constrained. The risk is daylight saving time. 02:15 does not exist on the spring-forward day, so the run may not fire at its usual time, and the equivalent UTC time shifts by an hour twice a year. Scheduling in UTC avoids both effects.