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)
│ │ │ │ │
* * * * * commandThe special characters
*— every value in the field.* * * * *runs every minute.,— list.0,15,30,45in the minute field runs on the quarter hour.-— range.9-17in the hour field means 9am through 5pm./— step.*/5in the minute field runs every five minutes.0/15starts at 0 and steps by 15.
Patterns you will actually use
*/5 * * * *— every five minutes0 * * * *— every hour, on the hour0 0 * * *— every day at midnight0 9 * * 1-5— every weekday at 9am0 0 1 * *— first day of every month at midnight0 0 * * 0— every Sunday at midnight
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:
- Quartz adds a seconds field at the front, so
0 */5 * * * ?in Quartz means every five minutes starting at second 0. - Quartz numbers days of the week 1–7 (Sunday = 1), not 0–6.
- Quartz requires
?in either day-of-month or day-of-week to make it explicit that the other field carries the constraint. Unix cron does not use?. - Quartz supports
L(last day),W(nearest weekday), and#(nth weekday of month). Unix cron does not.
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)
│ │ │ │ │
* * * * * 명령어특수 문자
*— 그 필드의 모든 값.* * * * *는 매 분 실행됩니다.,— 목록. 분 필드의0,15,30,45는 15분 간격으로 실행됩니다.-— 범위. 시 필드의9-17은 오전 9시부터 오후 5시까지를 의미합니다./— 간격. 분 필드의*/5는 5분마다 실행됩니다.0/15는 0에서 시작해 15씩 증가합니다.
실제로 쓰게 되는 패턴
*/5 * * * *— 5분마다0 * * * *— 매시 정각0 0 * * *— 매일 자정0 9 * * 1-5— 평일 오전 9시0 0 1 * *— 매월 1일 자정0 0 * * 0— 매주 일요일 자정
일 / 요일 동시 지정의 함정
일 필드와 요일 필드를 둘 다 제한하면(둘 다 *가 아니면), 전통적인 유닉스 cron은 두 조건 중 하나라도 맞는 시점에 작업을 실행합니다. AND가 아니라 OR입니다. 0 0 1 * 1은 "매월 1일 자정 또는 매주 월요일 자정"을 의미하지, "1일이면서 동시에 월요일인 날"을 의미하지 않습니다. AND 의미를 원한다면 둘 중 한 필드만 제한하세요.
유닉스 cron과 Quartz의 차이
스프링을 비롯한 많은 자바 시스템에서 쓰는 Quartz 스케줄러는 cron처럼 생겼지만 필드 수가 6~7개이고 요일 번호도 다릅니다. 기억해 둘 만한 차이:
- Quartz는 맨 앞에 초 필드를 추가합니다. Quartz의
0 */5 * * * ?은 매 5분의 0초에 실행됩니다. - Quartz는 요일을 1–7 (일요일 = 1)로 번호 매깁니다. 0–6이 아닙니다.
- Quartz는 일과 요일 중 한쪽에 반드시
?를 넣어 어느 쪽이 제약을 가지는지 명시하도록 요구합니다. 유닉스 cron에는?가 없습니다. - Quartz는
L(마지막 날),W(가장 가까운 평일),#(N번째 요일) 같은 추가 문자를 지원합니다. 유닉스 cron은 지원하지 않습니다.
사이트의 cron 도구는 유닉스 스타일 표현식을 파싱해서 다음 실행 시각 몇 개를 로컬 시간대로 보여줍니다. 운영에 반영하기 전에 스케줄을 검증하는 가장 빠른 방법입니다.