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
-
Install
django-celery-beatand apply migrations:/opt/venvs/app/bin/pip install 'django-celery-beat==2.6.*' /opt/venvs/app/bin/python manage.py migrate django_celery_beat -
Add it to
INSTALLED_APPSand configure Celery to use the database scheduler inapp/settings/prod.py:INSTALLED_APPS = [ # ... "django_celery_beat", ] CELERY_BEAT_SCHEDULER = "django_celery_beat.schedulers:DatabaseScheduler" -
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.targetStateDirectory=is how systemd creates/var/lib/celery-beat/with the right owner — no manualmkdirpluschowndance. -
Enable and start:
sudo systemctl daemon-reload sudo systemctl enable --now celery-beat.service journalctl -u celery-beat -n 40 --no-pager -
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", ) -
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 onExecStartPre=:ExecStartPre=/usr/local/bin/celery-beat-lock acquire ExecStopPost=/usr/local/bin/celery-beat-lock releasecelery-beat-lockis a small script thatSETNXs 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 anddjango-celery-beatat 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’stimezone, 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 setevery_minute_run=Falseon 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.