Skip to content
SpringCronTools

guide

Spring Cron vs Unix Cron: What Differs

Unix cron (crontab, CronJob, most cloud schedulers) and Spring @Scheduled look similar but are not the same language. Pasting a crontab line into @Scheduled is the most common mistake in Spring scheduling.

Five fields vs six fields

Unix cron uses minute hour day-of-month month day-of-week. Spring adds a leading seconds field. A crontab line "0 9 * * 1-5" therefore means something completely different in Spring: it is rejected because it only has five fields.

Unix:  0 9 * * 1-5
Spring: 0 0 9 ? * 1-5

Day fields: OR vs AND

In Unix cron, if both day-of-month and day-of-week are restricted, a date matching either one triggers the job. In Spring both fields must match.

This is the classic "it ran on the wrong day" bug: "0 0 9 1 * MON" in Spring means the 1st AND a Monday, not the 1st OR any Monday.

L, W and # are Spring/Quartz only

Unix cron has no concept of "last day of month" or "first Monday". Spring supports L, LW, <n>W and <dow>#<n> because it follows the Quartz day syntax.

Last day of the month:
  Spring: 0 0 0 L * *
  Unix:   not expressible — use 0 0 28-31 * * with a runtime guard

Converting safely

Conversion is lossless only when the seconds field is 0 and no Spring/Quartz-only day construct is used. If the seconds field is non-zero, or the expression uses L, W or #, no Unix equivalent exists and the schedule has to change.

Use the converter to see exactly which of the two cases you are in.

Summary table

Everything that differs, in one place. Spring is the right-hand column.

Summary table: Unix cron compared with Spring
DifferenceUnix cronSpring
Field count56 (seconds first)
Hour range0-230-23
Day-of-week numbers0-7, 0 = Sunday0-7, 0 and 7 = Sunday
Both day fields restrictedOR — either may matchAND — both must match
L / LW / W / #not supportedsupported
Macros@daily, @hourly@daily, @hourly

nextRelated guides

nextRelated tools

Questions

Can I copy a crontab expression into @Scheduled?

Only after adding a seconds field and checking the day semantics. "0 9 * * 1-5" becomes "0 0 9 ? * 1-5".

Why does my Spring job run less often than my Unix cron job?

Most likely because both day fields are restricted. Unix treats them as OR, Spring requires both.

referencesSources

A Spring cron expression is six space-separated fields — second, minute, hour, day-of-month, month, day-of-week — read left to right.
Field order per the CronExpression reference; the wording here is this site's, not a quotation from it.

Found a rule this site gets wrong? The parser is one file: the six-field reference lists every construct it accepts.