Skip to content

Crontab helper

cronbeat crontab rewrites the job lines of a crontab so that each one runs through cronbeat run. It is a convenience for many jobs: you can always edit the crontab by hand instead, see The run command.

Terminal window
cronbeat crontab plan # shows what would change, writes nothing
cronbeat crontab apply # backup, write, read back and verify
cronbeat crontab undo # restore the backup of the last apply

By default it works on the crontab of the current user (crontab -l and crontab -). Use --file for a file instead:

Terminal window
cronbeat crontab plan --file /etc/cron.d/myapp --cron-d
cronbeat crontab apply --file /etc/cron.d/myapp --cron-d

System crontabs (/etc/crontab and files in /etc/cron.d) have a user column after the schedule. --cron-d makes the helper understand it and keep it. --cron-d needs --file, and --file on /etc/crontab or /etc/cron.d/... without --cron-d is refused. The helper does not touch /etc/cron.hourly and the other directories.

SHELL=/bin/bash
CRON_TZ=Europe/Istanbul
# nightly backup
0 2 * * * /opt/backup.sh --full >> /var/log/backup.log 2>&1
@daily /usr/local/bin/cleanup.sh
@reboot /opt/start.sh
30 4 * * * date +%F > /tmp/last

cronbeat crontab plan reports (shortened):

line 5: wrap as "backup"
- 0 2 * * * /opt/backup.sh --full >> /var/log/backup.log 2>&1
+ 0 2 * * * /usr/local/bin/cronbeat run --name backup --schedule '0 2 * * *' --timezone Europe/Istanbul -- /bin/bash -c '/opt/backup.sh --full >> /var/log/backup.log 2>&1'
line 6: wrap as "cleanup"
line 7: skip, @reboot has no schedule
line 8: skip, unescaped % (cron turns it into a newline)
2 wrapped, 2 skipped, 4 unchanged.
  • Comments, blank lines and environment lines (NAME=value) are never touched.
  • The original command becomes one quoted argument of a shell: /bin/sh -c '<your command>', or the shell of a SHELL= line above it. Pipes, &&, redirections and quotes behave as before. A single quote inside is escaped.
  • The schedule is taken from the line. @daily and the other macros are converted to five fields (@hourly, @daily, @midnight, @weekly, @monthly, @yearly, @annually). A CRON_TZ= line above sets --timezone for the jobs below it. Without it, the zone of the server is detected when the job runs, see Time zones.
  • The job name is the base name of the command without a script extension (/opt/backup.sh becomes backup; for python3 /opt/report.py it is report). If two jobs would get the same name, the later one gets a stable suffix made from a hash of its schedule and command. Names used by lines that already run cronbeat are not reused.
  • The path of cronbeat is absolute (where the running binary is), because the PATH of cron is short.
  • Idempotent. A line that already uses cronbeat run is skipped, so running apply twice changes nothing.
  • The API key is never written into the crontab.

Each skipped line stays as it was, with the reason in the plan:

Line Why
@reboot ... There is no schedule to monitor.
A command with an unescaped % Cron turns % into a newline and sends the rest as input, quoting cannot repair that. Wrap such a line by hand, or escape the % with a backslash.
A line that is not valid Wrong number of fields or characters that are not part of a cron field.
A command that starts with - It would look like a flag.
A line that already uses cronbeat Already wrapped.
A line that would become longer than 1000 bytes Some cron implementations ignore longer lines.

apply saves the current content to ~/.local/state/cronbeat/crontab-backups/ (or $XDG_STATE_HOME/cronbeat/crontab-backups/) with mode 0600, writes the new content, reads it back and compares it with what it intended to write. A file given with --file is replaced atomically. It prints the backup file and the exact undo command.

undo restores the backup of the last apply, but only while the crontab is still exactly what apply wrote. If you or another tool changed it since, undo refuses and cronbeat crontab undo --force restores the backup anyway (your later changes are lost, so look first).

The helper wraps the jobs, it cannot give them the key. Make the key available to the user that runs them:

  • cronbeat configure as that user (writes ~/.config/cronbeat/key, or /etc/cronbeat/key as root), or
  • a CRONBEAT_API_KEY=cb_agent_... line near the top of the user’s crontab, or
  • --key-file in the lines you wrap by hand.

For a file in /etc/cron.d with a user other than root, /etc/cronbeat/key (mode 0600, owner root) is not readable by that user. Run cronbeat configure as that user, or create a key file for it. Files in /etc/cron.d are normally readable by everyone, so do not put CRONBEAT_API_KEY= in them.

Then check one job by running its wrapped line by hand with --verbose, and look at the Monitors list: the jobs appear after their first run (CronBeat creates a monitor on the first event of a job). Jobs that you want to appear immediately can be run once by hand.

  • It rewrites lines, it does not understand every dialect of cron. Read the plan before apply.
  • The helper is tested with generated crontab files and a fake crontab command, not against every cron implementation. Run plan on your real crontab and keep the backup until you have seen the first runs arrive.