<@ULVA73B9P> How to deploy a flow with Cron
# ask-marvin
h
@Marvin How to deploy a flow with Cron
m
thought for 341.0 seconds
Here’s how to deploy a flow on a cron schedule in Prefect 3.x. I’ll show both the Python API and the CLI, plus notes on timezones and workers. Assumptions: Prefect 3.x Python API - Quick option (no timezone control): pass a cron string directly to deploy
Copy code
from prefect import flow

@flow
def my_flow():
    print("hello from cron")

my_flow.deploy(
    name="hourly",
    work_pool_name="my-pool",
    cron="0 * * * *",  # top of every hour, UTC by default
)
- With timezone or advanced control: use a Schedule via prefect.schedules.Cron
Copy code
from prefect import flow
from prefect.schedules import Cron

@flow
def my_flow():
    print("hello from cron")

my_flow.deploy(
    name="hourly-nyc",
    work_pool_name="my-pool",
    schedule=Cron("0 * * * *", timezone="America/New_York"),
)
- From source (e.g., repo/project) with a Cron schedule
Copy code
from prefect.flows import Flow
from prefect.schedules import Cron

Flow.from_source(
    ".",  # or a git URL / storage
    "flows/my_flow.py:my_flow",
).deploy(
    name="hourly-utc",
    work_pool_name="my-pool",
    schedules=[Cron("0 * * * *", timezone="UTC")],
)
CLI - One-shot deploy with a cron schedule and timezone
Copy code
prefect deploy ./flows/my_flow.py:my_flow -n hourly-nyc --work-pool my-pool --cron "0 * * * *" --timezone "America/New_York"
- Or, deploy first, then add a schedule to an existing deployment
Copy code
# Create deployment
prefect deploy ./flows/my_flow.py:my_flow -n hourly --work-pool my-pool

# Attach cron schedule
prefect deployment schedule create "my_flow/hourly" --cron "0 * * * *" --timezone "America/New_York"
Make sure a worker is running - A deployment won’t run unless a worker is polling the work pool.
Copy code
# Create a local (process) work pool if you don’t have one
prefect work-pool create "my-pool" --type process

# Start a worker to poll the pool
prefect worker start --pool my-pool
Cron and timezone notes - Prefect uses standard 5-field cron (minute hour day-of-month month day-of-week), via croniter. - Use IANA timezones like America/New_York or Europe/Berlin for DST-aware schedules. - The Cron constructor supports
day_or
if you need to control how day-of-month and day-of-week interact (defaults to OR). Docs - Create schedules: Create schedules - API reference (schedules): prefect.schedules API - Manage deployment schedules via CLI: run
prefect deployment schedule create --help
If you can share your desired schedule expression and timezone, I can tailor the exact commands/snippet for your setup.