Skip to content

Scheduling periodic Celery tasks with Beat and django-celery-beat

Run Celery Beat as a separate systemd service, store the schedule in the database for live edits, and prevent duplicate dispatches in a multi-host deployment.

Celery Beat is the scheduler that turns “send the daily report at 06:00” into “queue this task on the broker every 24 hours.” It is a separate, single-instance process — never two — and it needs its own systemd unit, its own restart policy, and explicit guards against running multiple copies. This article wires Beat with django-celery-beat so schedules live in the database and are editable without a deploy, and adds a single-instance guard for multi-host setups.

How to verify

Confirm Beat is the only scheduler running and that recent dispatches actually landed:

systemctl status celery-beat.service --no-pager | head
ps -eo pid,etime,cmd | grep '[c]elery.*beat' | head
redis-cli -h 127.0.0.1 -n 0 LLEN celery
journalctl -u celery-beat -n 100 --no-pager | grep -E 'Scheduler|Sending due task'
ls -la /var/lib/celery-beat/celerybeat-schedule*

Two Beat processes on different hosts is the most common source of duplicate-execution bugs. The ps line should return one PID, not two.

What’s happening

Beat is a single-process scheduler that wakes up on the schedule tick, looks at the schedule definition, and dispatches due tasks to the broker. The schedule itself can be defined three ways: in code via app.conf.beat_schedule, in a SQLite/file-backed celerybeat-schedule file, or in the database via django-celery-beat. The third option is the one that scales operationally — admins can edit cron expressions in Django admin without a deploy, and there’s a single source of truth.

Beat must not run twice. Two Beat processes will both dispatch the same task at the same time and you will send two copies of the daily report. There’s no built-in coordination — the design assumes you run exactly one. In a multi-host deployment, this means picking one host and only that host runs Beat; or using a tool like redis-lock or a systemd OnFailure= chain to ensure failover is mutually exclusive.

The state Beat keeps between ticks (last-run timestamps) lives in either a file or a database table. With django-celery-beat, the django_celery_beat_periodictask table holds the schedule and django_celery_beat_periodictasks has a single row whose last_update is the “table dirty” flag — Beat polls it to know when to reload.

The procedure

  1. Install django-celery-beat and apply migrations:

    /opt/venvs/app/bin/pip install 'django-celery-beat==2.6.*'
    /opt/venvs/app/bin/python manage.py migrate django_celery_beat
  2. Add it to INSTALLED_APPS and configure Celery to use the database scheduler in app/settings/prod.py:

    INSTALLED_APPS = [
        # ...
        "django_celery_beat",
    ]
    CELERY_BEAT_SCHEDULER = "django_celery_beat.schedulers:DatabaseScheduler"
  3. Create /etc/systemd/system/celery-beat.service:

    [Unit]
    Description=Celery Beat scheduler
    After=network.target redis-server.service postgresql.service
    Requires=redis-server.service
    
    [Service]
    Type=simple
    User=deploy
    Group=deploy
    WorkingDirectory=/var/www/app
    EnvironmentFile=/etc/celery-app/env
    StateDirectory=celery-beat
    StateDirectoryMode=0750
    ExecStart=/opt/venvs/app/bin/celery -A app.celery beat \
        --schedule=/var/lib/celery-beat/celerybeat-schedule \
        --pidfile=/run/celery-beat.pid \
        --loglevel=INFO
    Restart=on-failure
    RestartSec=5
    
    [Install]
    WantedBy=multi-user.target

    StateDirectory= is how systemd creates /var/lib/celery-beat/ with the right owner — no manual mkdir plus chown dance.

  4. Enable and start:

    sudo systemctl daemon-reload
    sudo systemctl enable --now celery-beat.service
    journalctl -u celery-beat -n 40 --no-pager
  5. Add a schedule via Django admin or programmatically:

    from django_celery_beat.models import CrontabSchedule, PeriodicTask
    sched, _ = CrontabSchedule.objects.get_or_create(
        minute="0", hour="6", day_of_week="*",
        day_of_month="*", month_of_year="*", timezone="UTC",
    )
    PeriodicTask.objects.get_or_create(
        crontab=sched,
        name="daily-report",
        task="app.tasks.send_daily_report",
    )
  6. In a multi-host setup, run Beat on exactly one host and guard with a peer-failover script. The minimal pattern is a BindsTo=-style coordination through a shared Redis lock acquired on ExecStartPre=:

    ExecStartPre=/usr/local/bin/celery-beat-lock acquire
    ExecStopPost=/usr/local/bin/celery-beat-lock release

    celery-beat-lock is a small script that SETNXs a key with a TTL and exits non-zero if it already exists.

Common pitfalls

  • Two Beat processes on different hosts: every task fires twice. There is no “leader election” in Beat — you must enforce single-instance via systemd, a HA tool, or a peer lock.
  • Mixing beat_schedule= in code and django-celery-beat at the same time: Beat reads from the configured scheduler only; entries in the other source are silently ignored, and operators waste an afternoon wondering why a schedule edit didn’t take.
  • A timezone mismatch between Django’s TIME_ZONE, Celery’s timezone, and the crontab schedule — symptoms are tasks firing N hours off. Pin everything to UTC and convert at display time.
  • Beat skips a tick on overload: if a task with last_run_at=now() takes longer than its interval, the next tick lands before the first completes. Either widen the interval or set every_minute_run=False on a soloable task.
  • Forgetting StateDirectory=: under restart, Beat reads an empty schedule file and decides every task is due “right now,” firing the full daily batch within one second. Use the systemd directive so the schedule file survives a restart with the right ownership.

For the worker side of the stack, see Celery with a Redis backend. Stack Harbor’s managed operations practice runs Beat as a dedicated systemd service on one designated host per environment, with a peer-failover script and alerts that fire if Beat is silent for more than 90 seconds — the single most common Celery outage we get paged for.