← Back to blog

Cron expressions, end to end

Cron has been scheduling jobs on Unix systems since 1975 and the syntax has not really changed since. It is also one of those things you can use for years without ever fully understanding it, because 0 * * * * seems to work and that is enough until it is not.

The five fields

A standard cron expression has five fields, separated by spaces:

┌─────── minute        (0 - 59)
│ ┌───── hour          (0 - 23)
│ │ ┌─── day of month  (1 - 31)
│ │ │ ┌─ month         (1 - 12)
│ │ │ │ ┌ day of week  (0 - 6, Sunday = 0)
│ │ │ │ │
* * * * *  command

The special characters

Patterns you will actually use

The day-of-week / day-of-month trap

If both the day-of-month and day-of-week fields are restricted (neither is *), traditional Unix cron runs the job when either condition matches, not both. 0 0 1 * 1 means "midnight on the 1st of every month, OR every Monday at midnight" — not "the 1st only if it is also a Monday." If you want the AND interpretation, restrict only one of the two fields.

Unix cron vs Quartz

Quartz, the scheduler used by Spring and many Java systems, looks like cron but has six or seven fields and a different week numbering. Differences worth remembering:

The cron tool on this site parses Unix-style expressions and shows you the next several execution times in your local timezone. That is the fastest way to sanity-check a schedule before you push it to production.


Cron 표현식, 처음부터 끝까지

Cron은 1975년부터 유닉스 시스템에서 작업 스케줄링을 담당해 왔고, 문법은 그때부터 거의 변하지 않았습니다. 그만큼 오랫동안 완전히 이해하지 못한 채로 써도 되는 도구이기도 합니다 — 0 * * * *가 그럭저럭 동작하니까, 그게 안 통하는 순간이 올 때까지는 별 문제 없이 굴러갑니다.

다섯 필드

표준 cron 표현식은 공백으로 구분된 다섯 필드로 이루어집니다.

┌─────── 분            (0 - 59)
│ ┌───── 시간          (0 - 23)
│ │ ┌─── 일            (1 - 31)
│ │ │ ┌─ 월            (1 - 12)
│ │ │ │ ┌ 요일         (0 - 6, 일요일 = 0)
│ │ │ │ │
* * * * *  명령어

특수 문자

실제로 쓰게 되는 패턴

일 / 요일 동시 지정의 함정

일 필드와 요일 필드를 둘 다 제한하면(둘 다 *가 아니면), 전통적인 유닉스 cron은 두 조건 중 하나라도 맞는 시점에 작업을 실행합니다. AND가 아니라 OR입니다. 0 0 1 * 1은 "매월 1일 자정 또는 매주 월요일 자정"을 의미하지, "1일이면서 동시에 월요일인 날"을 의미하지 않습니다. AND 의미를 원한다면 둘 중 한 필드만 제한하세요.

유닉스 cron과 Quartz의 차이

스프링을 비롯한 많은 자바 시스템에서 쓰는 Quartz 스케줄러는 cron처럼 생겼지만 필드 수가 6~7개이고 요일 번호도 다릅니다. 기억해 둘 만한 차이:

사이트의 cron 도구는 유닉스 스타일 표현식을 파싱해서 다음 실행 시각 몇 개를 로컬 시간대로 보여줍니다. 운영에 반영하기 전에 스케줄을 검증하는 가장 빠른 방법입니다.