Skip to content

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.

A timer starts a service of Type=oneshot. Put cronbeat run in ExecStart.

/etc/systemd/system/nightly-backup.service
[Unit]
Description=Nightly backup
[Service]
Type=oneshot
User=backup
ExecStart=/usr/local/bin/cronbeat run --name nightly-backup --schedule "0 2 * * *" -- /opt/backup.sh --full
EnvironmentFile=/etc/cronbeat/nightly-backup.env
/etc/systemd/system/nightly-backup.timer
[Unit]
Description=Nightly backup
[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true
[Install]
WantedBy=timers.target
Terminal window
sudo systemctl daemon-reload
sudo systemctl enable --now nightly-backup.timer
sudo systemctl start nightly-backup.service # run once now, to create the monitor
journalctl -u nightly-backup.service # warnings and the job's output

systemd does not read ~/.config for you and gives services a clean environment. Pick one:

  • An environment file (as above). /etc/cronbeat/nightly-backup.env contains one line, CRONBEAT_API_KEY=cb_agent_..., and has mode 0600 and owner root (systemd reads it as root). The service gets the key as CRONBEAT_API_KEY; cronbeat run removes 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.key and add Environment=CRONBEAT_KEY_FILE=/etc/cronbeat/backup.key to 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.

  • Keep the schedule in sync. OnCalendar decides when systemd starts the job, --schedule is 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 $VARIABLE in ExecStart.
  • 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 cronbeat go to stderr and so to the journal, not to a mailbox.
  • Hardening options such as ProtectHome=true hide ~/.config and ~/.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; point XDG_CACHE_HOME at a writable directory with Environment=.
  • Stopping the service sends SIGTERM, which cronbeat forwards to your command, and the run is reported as failed with the signal.

The command is a single static file without dependencies, so it works in nearly any image. There are three ways to use it.

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:latest
FROM debian:bookworm-slim
RUN 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 sh
COPY report.sh /app/report.sh
ENTRYPOINT ["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.

The binary does not depend on the image, so you can mount it read-only:

Terminal window
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.sh
  • cronbeat becomes the main process of the container and forwards docker stop (SIGTERM) to your command.
  • Give the key with --env-file or a mounted file, not with -e CRONBEAT_API_KEY=... on the command line (it shows in ps and docker inspect). A mounted key file must have mode 0600 or 0400. Docker and Kubernetes secrets are often mounted with mode 0444, and cronbeat refuses such a file; set the mode or use the environment variable.
  • A container has no /etc/timezone or /etc/localtime in many images, so the time zone falls back to UTC. Pass --timezone (or TZ) when your schedule is in another zone.
  • Alpine and BusyBox based images, Docker secrets and minimal scratch images follow the same rules, but they were not tested for this documentation: run cronbeat check inside the image once. The installer needs curl or wget with HTTPS support, tar and sha256sum.
  • For jobs that run as Kubernetes CronJobs use the Kubernetes agent, which needs no changes to the image.

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.