Blog
Guides

How to run a Python script every hour

DK
Dan Kowalsky
Founder ·

Hourly jobs are the workhorse of internal automation: sync new orders, refresh a dashboard, check a feed for changes. Getting one to run every hour is easy. Getting it to run every hour reliably, without overlapping runs or silent failures, takes a little more thought. Here are the main ways to do it, then the habits that keep an hourly job healthy.

The cron expression for "every hour, on the hour" is 0 * * * *. Use 15 * * * * to run at quarter past instead, which helps if lots of jobs on the same machine fire at the top of the hour. More expressions are in the cron cheat sheet.

1. cron (Linux and macOS)

Run crontab -e and add:

0 * * * * /home/me/app/.venv/bin/python /home/me/app/sync.py >> /home/me/app/sync.log 2>&1

Use absolute paths, point at your virtualenv's Python, and send output to a file: cron has almost no environment and discards output by default. The machine has to be on and awake at each hour.

2. Task Scheduler (Windows)

schtasks /create /tn "Hourly sync" /tr "C:\Python313\python.exe C:\app\sync.py" /sc hourly /st 00:00

/sc hourly with a start time on the hour keeps it on the hour. In the Task Scheduler app, the same thing is a daily trigger with Repeat task every: 1 hour for a duration of Indefinitely.

3. GitHub Actions

on:
  schedule:
    - cron: "0 * * * *"

No machine to keep on, and the logs are in the Actions tab. Two things to know for hourly jobs: runs can start several minutes late when GitHub is busy, and on the hour is the busiest time, so a minute like :17 tends to start closer to on time. Scheduled workflows in public repos are also turned off after 60 days without activity.

4. A loop in Python

import time

import schedule


def sync():
    ...


schedule.every().hour.at(":00").do(sync)

while True:
    schedule.run_pending()
    time.sleep(30)

The schedule library reads nicely, and so does the even simpler while True: sync(); time.sleep(3600). But both only work while the process is alive. If it crashes or the machine restarts, the job stops until someone notices, so you still need a server and something like systemd or a process manager to keep it running. A bare sleep(3600) also drifts: if the job takes 4 minutes, the next run is 64 minutes later.

5. Callables

Write the job as a function and push it. Then either pick Every hour in the function's Schedule… menu, or put the schedule in the code:

from callables import deploy


@deploy(title="Hourly order sync", secrets=["SHOP_TOKEN"], schedule="0 * * * *")
def sync_orders(lookback_minutes: int = 90) -> str:
    """Copies new orders into the warehouse sheet."""
    ...
    return f"Synced {count} orders"

There's no machine to keep awake, the SHOP_TOKEN secret stays out of the code, and every run is saved with its inputs, logs and result, so you can see at a glance which hour failed and why. Runs last 5 minutes by default, and timeout raises that to 30 minutes on Hobby or 2 hours on Pro, which is plenty for most hourly jobs. The same function is also a form, so someone can run a sync by hand between hours.

Keep an hourly job healthy

Make it safe to run twice. Jobs get retried, run late or overlap. Use a lookback window wider than the interval (the 90 minutes above), and upsert by ID rather than appending, so the same order processed twice is still one row.

Don't let runs overlap. If a run can sometimes take longer than an hour, two copies can end up working on the same data. On Linux, flock skips a run while the last one still holds the lock:

0 * * * * flock -n /tmp/sync.lock /home/me/app/.venv/bin/python /home/me/app/sync.py >> /home/me/app/sync.log 2>&1

Better still, find out why the job takes so long, or run it less often.

Make failures loud. The worst hourly job is the one that quietly stopped working three weeks ago. Exit with an error when something goes wrong, rather than catching everything and printing, so whatever runs the job can see it failed. Then make sure failures reach a person: a cron MAILTO, GitHub's failure emails, or a glance at the run history.

Keep secrets out of the script. An API key in sync.py ends up in backups, screenshots and git history. Read it from the environment instead. See keeping API keys out of Python scripts.

For daily, weekly and other schedules, and a side-by-side comparison of every method, see how to run a Python script on a schedule.

Turn your scripts into tools your team can run

Add @deploy to a Python function and push. Your team gets a form, schedules and run history.

See how it works

Keep reading