What it means
crontab -e and crontab <file> parse the whole file before installing it. One bad line and nothing is installed:
crontab: installing new crontab
"/tmp/crontab.Pk3yQa/crontab":4: bad day-of-week
errors in crontab file, can't install.
Do you want to retry the same edit? (y/n)The number after the file name is the line, and the words after it name the field that failed: bad minute, bad hour, bad day-of-month, bad month, bad day-of-week or bad command. Your previous crontab keeps running.
Common causes
1. Quartz or Spring syntax. 0 0 2 ? * MON is valid for Quartz, not for cron: ? gives bad day-of-month, and L, W or # are refused the same way. A leading seconds field is worse, since it often parses and shifts every field by one.
2. A value out of range. Minute 0 to 59, hour 0 to 23, day of month 1 to 31, month 1 to 12, day of week 0 to 7. 60, 24 or */60 in the hour field fail.
3. An environment line written like shell. export PATH=/usr/local/bin:/usr/bin:/bin gives bad minute. Crontabs take NAME=value, without export.
4. A line with no command. Five fields and nothing after them give bad command, often a line split in two by the editor.
5. A missing final newline. Debian's crontab refuses the file with new crontab file is missing newline before EOF, can't install.
6. Syntax from another scheduler. @every 5m belongs to Go libraries, rate(5 minutes) to AWS. Cron knows @reboot, @hourly, @daily, @weekly, @monthly and @yearly.
How to fix
- Read the line number in the message and open that line.
- Rewrite the schedule as five fields:
0 2 * * 1for 02:00 every Monday. - Replace
export NAME=valuewithNAME=value. - Rejoin split lines and make sure the file ends with a newline.
- With
crontab -e, answeryto retry the same edit. With a file, fix it and runcrontab fileagain, then check withcrontab -l.
Is it just you?
Paste the five fields into the cron expression generator: it explains each field and lists the next run times in your timezone, before you install anything.
Get alerted next time
- Use the same five-field expression and timezone for a healthcheck in cron mode: Hyperping expects a ping at every run the expression produces.
- If an edit is refused and the job stops being installed, or runs at the wrong time, the missing ping alerts you once the grace period passes.
- See how it fits with other scheduled jobs on cron job monitoring.
FAQ
Is my old crontab lost after errors in crontab file, can't install? ▼
No. crontab installs the new file only if every line parses, so the previous version keeps running. With crontab -e, answer y to Do you want to retry the same edit? to get your changes back in the editor; answering n discards them.
Does cron support seconds or the ? and L characters? ▼
No. Standard cron (Debian's cron, cronie) has five fields, minute to day of week. A seconds field, ?, L, W and # come from Quartz and Spring. ? or L in a day field is refused with bad day-of-month or bad day-of-week, while a sixth field is read as the start of the command.
Is 7 a valid day of week in crontab? ▼
Yes. Both 0 and 7 mean Sunday, and names such as MON or sun work too. 8 is refused with bad day-of-week.