Overview

Cron expressions are used everywhere: Linux scheduled tasks, Spring Boot scheduling, cloud automation, and CI jobs. This guide explains the syntax and provides ready-to-use examples.

Cron Expression Structure

A standard Unix cron expression has five fields. Quartz, commonly used in Java applications, uses six or seven fields.

Unix Five-Field Format

minute  hour  day-of-month  month  day-of-week
  *      *         *          *         *

Quartz Six-Field Format

second  minute  hour  day-of-month  month  day-of-week
  *       *      *         *          *         *

Field Values

FieldUnix positionQuartz positionAllowed values
Second1st0–59
Minute1st2nd0–59
Hour2nd3rd0–23
Day of month3rd4th1–31
Month4th5th1–12 or JAN–DEC
Day of week5th6th0–6 (0 = Sunday) or SUN–SAT

Special Characters

CharacterMeaningExample
*Any value* in minute means every minute
,List of values1,15,30 means minutes 1, 15, and 30
-Range9-17 means hours 9 through 17
/Start and interval0/5 means every 5 units starting at 0
?No specific valueUsed in day-of-month or day-of-week
LLast dayL in day-of-month means last day of month
WNearest weekday15W means nearest weekday to the 15th
#Nth weekday2#1 means first Monday of the month

? is only used in the day-of-month and day-of-week fields because these two fields can conflict. If one is specified, the other should usually be ?.

Common Quartz Examples

ScheduleQuartz expression
Every day at 10:15 AM0 15 10 ? * *
Every day at 10:00, 14:00, and 16:000 0 10,14,16 * * ?
Every hour at minute 40 from 9 AM to 5 PM0 40 9-17 * * ?
Every 30 minutes from 10 AM to 4 PM0 0/30 10-16 ? * 2
15th day of every month at 10:15 AM0 15 10 15 * ?
Last day of every month at 10:15 AM0 15 10 L * ?
Every Wednesday in March at 2:00 PM and 2:30 PM0 0,30 14 ? 3 WED

Common Unix Examples

ScheduleUnix expression
Every day at 9:30 AM30 9 * * *
Every 5 minutes*/5 * * * *
Every Monday at 8:00 AM0 8 * * 1
First day of every month at midnight0 0 1 * *

Linux Crontab Example

# Edit the current user's cron jobs
crontab -e

# Add a job: back up the database every day at 2:30 AM
30 2 * * * /home/user/backup.sh >> /var/log/backup.log 2>&1

# List current cron jobs
crontab -l

# Remove all cron jobs
crontab -r

Always redirect output to a log file. Otherwise, job output is lost and troubleshooting becomes difficult.

Common Mistakes

  • 0 and 7 both mean Sunday in some systems, but behavior can vary. Use 0 for consistency.
  • Do not specify both day-of-month and day-of-week with fixed values. Use ? for one of them.
  • Quartz and Unix have different field counts. Spring Boot uses the six-field Quartz format by default, where the first field is seconds.
  • Cron does not use the same environment as your shell. Use absolute paths or set PATH inside crontab.