systemd timers and Docker
cronbeat run works wherever something can start a command: cron, a systemd timer, a shell script or a container. There
is nothing to configure in these environments except two things: the command line and where the key comes from.
systemd timer
Section titled “systemd timer”A timer starts a service of Type=oneshot. Put cronbeat run in ExecStart.
[Unit]Description=Nightly backup
[Service]Type=oneshotUser=backupExecStart=/usr/local/bin/cronbeat run --name nightly-backup --schedule "0 2 * * *" -- /opt/backup.sh --fullEnvironmentFile=/etc/cronbeat/nightly-backup.env[Unit]Description=Nightly backup
[Timer]OnCalendar=*-*-* 02:00:00Persistent=true
[Install]WantedBy=timers.targetsudo systemctl daemon-reloadsudo systemctl enable --now nightly-backup.timersudo systemctl start nightly-backup.service # run once now, to create the monitorjournalctl -u nightly-backup.service # warnings and the job's outputWhere the key comes from
Section titled “Where the key comes from”systemd does not read ~/.config for you and gives services a clean environment. Pick one:
- An environment file (as above).
/etc/cronbeat/nightly-backup.envcontains one line,CRONBEAT_API_KEY=cb_agent_..., and has mode 0600 and ownerroot(systemd reads it as root). The service gets the key asCRONBEAT_API_KEY;cronbeat runremoves it from the environment of your command, so your job does not see it. - A key file that the service user can read. Create it with
sudo -u backup cronbeat configure --key-file /etc/cronbeat/backup.keyand addEnvironment=CRONBEAT_KEY_FILE=/etc/cronbeat/backup.keyto the unit. The file must have mode 0600 (or 0400).
Do not write the key into the unit file: unit files are readable by every user and systemctl show prints them.
Things to know
Section titled “Things to know”- Keep the schedule in sync.
OnCalendardecides when systemd starts the job,--scheduleis what CronBeat expects. They are two places that must say the same. A timer in another time zone than the server needs--timezone. %in unit files is a specifier: write%%for a literal percent sign, and$$for a literal dollar sign, because systemd expands$VARIABLEinExecStart.- Persistent=true makes systemd run a missed timer when the machine comes back. If the machine was off at the scheduled time, the monitor may already be late or down when the catch-up run arrives, and the run then recovers it.
- No mail. Warnings from
cronbeatgo to stderr and so to the journal, not to a mailbox. - Hardening options such as
ProtectHome=truehide~/.configand~/.cache. Use a key file or environment file outside the home directory. If the warning state cannot be written (ProtectSystem=strict), warnings appear on every run instead of once per 6 hours; pointXDG_CACHE_HOMEat a writable directory withEnvironment=. - Stopping the service sends
SIGTERM, whichcronbeatforwards to your command, and the run is reported as failed with the signal.
Docker
Section titled “Docker”The command is a single static file without dependencies, so it works in nearly any image. There are three ways to use it.
Wrap docker run on the host
Section titled “Wrap docker run on the host”The simplest: install cronbeat on the host and let it wrap the container. The exit code of docker run is the exit code
of the container.
0 3 * * * /usr/local/bin/cronbeat run --name nightly-report --schedule '0 3 * * *' -- /usr/bin/docker run --rm myorg/report:latestInstall it in the image
Section titled “Install it in the image”FROM debian:bookworm-slimRUN apt-get update \ && apt-get install -y --no-install-recommends ca-certificates curl \ && rm -rf /var/lib/apt/lists/* \ && curl -fsSL https://docs.cronbeat.dev/install.sh | CRONBEAT_INSTALL_DIR=/usr/local/bin shCOPY report.sh /app/report.shENTRYPOINT ["cronbeat", "run", "--name", "nightly-report", "--schedule", "0 3 * * *", "--"]CMD ["/app/report.sh"]Pin the version with CRONBEAT_VERSION for reproducible builds. The image needs CA certificates (the ca-certificates
package), otherwise HTTPS to api.cronbeat.dev fails with a certificate warning.
Mount the binary
Section titled “Mount the binary”The binary does not depend on the image, so you can mount it read-only:
docker run --rm \ -v /usr/local/bin/cronbeat:/usr/local/bin/cronbeat:ro \ --env-file /etc/cronbeat/nightly-report.env \ --entrypoint cronbeat \ myorg/report:latest run --name nightly-report --schedule "0 3 * * *" -- /app/report.shDocker notes
Section titled “Docker notes”cronbeatbecomes the main process of the container and forwardsdocker stop(SIGTERM) to your command.- Give the key with
--env-fileor a mounted file, not with-e CRONBEAT_API_KEY=...on the command line (it shows inpsanddocker inspect). A mounted key file must have mode 0600 or 0400. Docker and Kubernetes secrets are often mounted with mode 0444, andcronbeatrefuses such a file; set the mode or use the environment variable. - A container has no
/etc/timezoneor/etc/localtimein many images, so the time zone falls back toUTC. Pass--timezone(orTZ) when your schedule is in another zone. - Alpine and BusyBox based images, Docker secrets and minimal
scratchimages follow the same rules, but they were not tested for this documentation: runcronbeat checkinside the image once. The installer needscurlorwgetwith HTTPS support,tarandsha256sum. - For jobs that run as Kubernetes CronJobs use the Kubernetes agent, which needs no changes to the image.
cron MAILTO
Section titled “cron MAILTO”Cron mails everything a job prints to the owner or to MAILTO. Warnings of cronbeat are printed to stderr, so they
end up there too, but at most once per kind of problem and 6 hours, so an unreachable CronBeat does not fill a mailbox. If you
want no mail from cron at all, set MAILTO="" at the top of the crontab; the monitor in CronBeat is then your alert.
See Troubleshooting for what the warnings mean.

