Skip to content

Run a Python Script on a Schedule

Deploy a Python script that runs on its own, every 10 minutes, every hour, or every morning at 8, without leaving your laptop on or setting up a server. Typical jobs: sync data between two services, send a daily report, scrape a page, clean up old records, or run a bot.

Note: this runs as a backend, which requires the Pro plan.

How it works

Your script runs in its own container that stays on. The schedule lives in your script: it does the work, waits, and does it again. You don’t need to configure anything on the platform.

Two rules keep it running:

  • Don’t let the script finish. When it exits normally, the backend stops until you deploy again. Keep it in a loop.
  • Catch errors inside the loop. A crash restarts the script after 10 seconds, but it’s cleaner to log the error and wait for the next run.

Option 1: every few minutes

The simplest way, with nothing to install. Save this as main.py:

import time
import traceback

EVERY_MINUTES = 10


def job():
    print("syncing…")


while True:
    try:
        job()
    except Exception:
        traceback.print_exc()
    time.sleep(EVERY_MINUTES * 60)

Option 2: at set times

For “every day at 08:00” or “every Monday”, use the schedule package. Add it to requirements.txt:

schedule

and write main.py:

import time
import traceback

import schedule


def daily_report():
    print("sending the daily report")


def safely(job):
    def run():
        try:
            job()
        except Exception:
            traceback.print_exc()
    return run


schedule.every().day.at("08:00").do(safely(daily_report))
schedule.every().hour.do(safely(lambda: print("hourly check")))

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

Times are in UTC. 08:00 means 08:00 UTC, so convert from your local time.

Deploy it

From the folder with main.py:

pbc deploy --name nightly-sync

The script is detected and started with python main.py. The version and dependencies are handled the same way as for any Python app. If your folder has several scripts, name the one to run:

pbc deploy --name nightly-sync --start "python sync.py"

In the portal, open your project’s Backends tab, click New Backend, and drop in the folder. The start command is shown before you create it, and you can change it on the backend’s Settings page.

The backend also gets a web address. A script that serves no pages doesn’t need it, so it’s fine that the address doesn’t answer.

Give it secrets

Put API keys in a .env next to the script. pbc deploy pushes it for you, and the script reads it from the environment:

import os

token = os.environ["SLACK_TOKEN"]

See Backend Environment Variables.

Check that it ran

Everything the script prints appears in the backend’s Logs page, straight away:

pbc logs backend --name nightly-sync -f

Change the schedule

Edit the script and run pbc deploy again. The new version replaces the old one. The loop restarts from the top, so a job set to run every hour runs its first time an hour after the deploy.

Only touching your PocketBase data?

If the job only reads or writes your own PocketBase data, you may not need a backend at all. PocketBase can run it on a cron schedule with cronAdd in a hook, with nothing extra to deploy.

Next steps