How to check a crontab
Paste a user crontab (the output of crontab -l) as it is. For /etc/crontab or a file in /etc/cron.d, switch on System crontab, because those lines carry a user name. Comments and blank lines are skipped, and settings such as MAILTO and CRON_TZ apply to the jobs after them.
The five fields are minute, hour, day of month, month and day of week, then the command; the generator page explains each field and the @ shortcuts. A single cron expression works too. Paste */15 9-17 * * 1-5 and you get its meaning and the next five runs.
On a system with cronie, crontab -T file tests a file's syntax before you install it.
What the cron parser checks
| Check | What goes wrong |
|---|---|
| A bare % in a command | cron ends the command at the first unescaped % and sends the rest to it as input, so date +%F breaks. Write \%. |
| Both day fields set | When neither starts with *, cron runs on days that match either field, so 0 9 1-7 * 1 runs on the 1st to the 7th and on every Monday. |
| Steps that do not divide evenly | */7 in the minute field runs at :56 and again at :00, four minutes later. |
| Six or seven fields | Quartz, Spring and AWS EventBridge add seconds or a year. A crontab line has five. |
| $ or ~ in a setting | cron does not expand them, so PATH=$HOME/bin:$PATH is used exactly as typed. |
| A comment after a setting | The comment becomes part of the value. After a quoted value, cronie stops reading the line as a setting. |
| Daylight saving | A fixed-time job in a skipped hour runs just after the change, and once in a repeated hour. A job like */15 2 * * * misses a skipped hour and runs twice in a repeated one. |
| The user field | Lines in /etc/crontab and /etc/cron.d need a user name between the schedule and the command. |
| Repeated lines | The same schedule and command twice runs the job twice. |
| Dates that never come | A schedule such as 0 0 30 2 * (30 February) never runs. |
The daylight saving check looks a year ahead in the zone each job runs in and covers every job that does not run in every hour. Those rules are cronie's for jobs in the server's own zone. A job with an hour step such as 0 */2 * * * follows the wall clock, so it loses or repeats a run when its hour is skipped or repeated. For a job under CRON_TZ, cronie's source says jobs in the hour of a clock change can be missed or run twice, so keep those jobs out of that hour. A job that runs in every hour keeps its interval through the change.
Settings in a crontab
A line of the form NAME=value sets a variable for the jobs below it. Spaces around the = are optional, and quotes keep leading or trailing spaces.
- SHELL: cron runs commands with /bin/sh unless you set another shell.
- MAILTO: where cron mails a job's output.
MAILTO=""turns the mail off. - CRON_TZ: on cronie, the time zone the job times are written in. Debian's cron has one time zone for every job, and a TZ setting there changes only what the commands see.
- RANDOM_DELAY: on cronie, starts jobs up to that many minutes late.
User and system crontabs
A user crontab, edited with crontab -e, has five schedule fields and then the command, and its jobs run as that user. /etc/crontab and the files in /etc/cron.d are system crontabs: a user name sits between the schedule and the command, as in * * * * * root touch /tmp/file.
The last line needs a newline
cron needs every line of a crontab to end with a newline. If the last line ends the file without one, cronie treats the crontab as at least partly broken and logs a warning. Pasting into a form usually drops a final newline, so this page cannot tell whether your file has one. If you write crontab files from a script, end them with a newline.
Sources
- crontab(5), cronie: settings, comments, the user field of system crontabs, % in commands and the final newline
- cron(8), cronie: how jobs are run around daylight saving changes
- crontab(5), Debian: settings are not expanded, and the cron daemon has one time zone
- cronie source (cron.c): CRON_TZ per job, clock changes and its note on job time zones
- cronie source (env.c): how a setting line is read, including quoted values