CodeCraft
← Back to Blog
DevOps2026-04-19·10 min read

Cron Expressions Explained: Scheduling Tasks Like a Pro

#cron#scheduling#linux#kubernetes#devops
C
CodeCraft Team· Engineering

Cron jobs power some of the most critical automation in modern infrastructure — from database backups and log rotation to report generation, cache warming, and API health checks. Yet the cron expression syntax, with its five cryptic fields, trips up even experienced developers. A misplaced asterisk can mean the difference between a job running every minute versus once a year.

This guide demystifies cron syntax, explains each field with practical examples, covers modern extensions like seconds fields and special characters, and shows you how to write, test, and debug cron expressions for Linux cron, Kubernetes CronJob, AWS EventBridge, and serverless functions.

Cron Syntax: The Five Fields

A standard cron expression consists of five fields separated by spaces:

┌───────────── minute (0–59)
│ ┌───────────── hour (0–23)
│ │ ┌───────────── day of month (1–31)
│ │ │ ┌───────────── month (1–12)
│ │ │ │ ┌───────────── day of week (0–7, 0 and 7 = Sunday)
│ │ │ │ │
* * * * *  command to execute

Quick reference — the most common patterns:

| Expression | Meaning |

|------------|---------|

| * * * * * | Every minute |

| 0 * * * * | Every hour (at minute 0) |

| 0 0 * * * | Daily at midnight |

| 0 0 * * 0 | Weekly on Sunday at midnight |

| 0 0 1 * * | Monthly on the 1st at midnight |

| 0 0 1 1 * | Yearly on Jan 1st at midnight |

| */5 * * * * | Every 5 minutes |

| 0 9-17 * * 1-5 | Every hour from 9 AM to 5 PM, weekdays only |

Special Characters: * , - / ? L W #

Cron supports several special characters that unlock powerful scheduling patterns:

* (Asterisk)** — Matches any value:

0 * * * *    → At minute 0 of every hour
* * * * *    → Every minute

`,` (Comma) — Specifies a list of values:

0 9,12,15 * * *    → At 9 AM, 12 PM, and 3 PM daily
* * * * 1,3,5      → Every minute on Mon, Wed, Fri

`-` (Hyphen) — Specifies a range:

0 9-17 * * *       → Every hour from 9 AM to 5 PM
0 0 * * 1-5        → Daily at midnight, weekdays only

`/` (Slash) — Specifies a step:

*/15 * * * *       → Every 15 minutes
0 */6 * * *        → Every 6 hours
0 0 1/7 * *        → Every 7 days starting from the 1st

`?` (Question mark) — Used in day-of-month and day-of-week to mean "no specific value" (Quartz scheduler / AWS EventBridge):

0 12 15 * ?        → At noon on the 15th of each month
0 12 ? * MON       → At noon every Monday

`L` (Last) — Last day of month or last occurrence of a weekday (Quartz):

0 0 L * ?          → At midnight on the last day of each month
0 0 ? * 5L         → At midnight on the last Friday of each month

`#` (Hash) — Nth weekday of the month (Quartz):

0 0 ? * 1#1        → At midnight on the first Monday of each month
0 0 ? * 5#3        → At midnight on the third Friday of each month

Month and Weekday Names

Instead of numbers, you can use three-letter names for months and weekdays in most implementations:

Months:

JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC

Weekdays:

SUN MON TUE WED THU FRI SAT

# Examples using names:
0 0 * JAN,JUL *     → At midnight in January and July
0 0 * * MON-FRI     → At midnight on weekdays
30 8 * * MON        → 8:30 AM every Monday

Day-of-week caveat: Different cron implementations handle Sunday differently:

- Linux cron: Sunday = 0 or 7 (both work)

- Quartz (Java): Sunday = 1, Saturday = 7

- AWS EventBridge: Sunday = 1, Saturday = 7

Always verify which implementation you're using!

Six-Field Cron (with Seconds)

Many modern schedulers extend the standard five-field cron with a seconds field, either at the beginning or end.

Quartz Cron (seconds first):

┌───────────── second (0–59)
│ ┌───────────── minute (0–59)
│ │ ┌───────────── hour (0–23)
│ │ │ ┌───────────── day of month (1–31)
│ │ │ │ ┌───────────── month (1–12)
│ │ │ │ │ ┌───────────── day of week (1–7, 1=Sunday)
│ │ │ │ │ │
* * * * * *

0 0 12 * * ?       → Every day at noon exactly
0 */30 * * * ?     → Every 30 minutes
0 0 9 * * MON-FRI  → 9 AM on weekdays

GitHub Actions uses a five-field cron (standard Unix format):

on:
  schedule:
    - cron: '0 2 * * *'    # Daily at 2 AM UTC
    - cron: '*/30 * * * *' # Every 30 minutes

Kubernetes CronJob also uses five-field cron:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: backup-job
spec:
  schedule: "0 3 * * *"   # Daily at 3 AM
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: backup
            image: backup-image:latest

Real-World Cron Patterns

Database backup (daily at 2 AM):

0 2 * * * /usr/local/bin/backup-db.sh

Weekly cleanup (every Sunday at 3 AM):

0 3 * * 0 /usr/local/bin/cleanup.sh

Business hours monitoring (every 5 minutes, Mon-Fri 8 AM – 8 PM):

*/5 8-20 * * 1-5 /usr/local/bin/health-check.sh

Monthly invoice generation (first day of month at 6 AM):

0 6 1 * * /usr/local/bin/generate-invoices.sh

Cache warming (every hour, slightly offset to avoid on-the-hour spikes):

15 * * * * /usr/local/bin/warm-cache.sh

Daily report sent to team (weekdays at 9 AM):

0 9 * * 1-5 /usr/local/bin/daily-report.sh

Log rotation (daily at midnight, keep 30 days):

0 0 * * * find /var/log/app -name "*.log" -mtime +30 -delete

Certificate renewal check (twice daily):

0 0,12 * * * certbot renew --quiet

Cron Troubleshooting: Why Your Job Isn't Running

1. Timezone mismatch: Cron runs in the system timezone, not your local timezone. Always check date on the server or configure the timezone explicitly:

# In /etc/crontab, you can set the timezone:
CRON_TZ=Asia/Shanghai
0 9 * * * /path/to/script.sh  # now runs at 9 AM Shanghai time

2. Environment variables: Cron runs in a minimal environment — PATH, HOME, and SHELL may not be what you expect. Always use absolute paths:

# Bad:
0 * * * * python script.py

# Good:
0 * * * * /usr/bin/python3 /home/user/scripts/script.py

3. No output capture: If a cron job fails silently, redirect logs:

0 * * * * /path/to/script.sh >> /var/log/myjob.log 2>&1

4. Day-of-month and day-of-week conflict: When both are non-*, many cron implementations use OR instead of AND:

0 0 1 * 1   # Runs on the 1st of the month OR every Monday (not AND)

5. Quartz vs Unix differences: If migrating between systems (e.g., Spring Boot to a Linux cron), verify:

- Seconds field presence

- Weekday numbering (0 vs 1 for Sunday)

- ? interpretation

Alternatives to Traditional Cron

Systemd timers (Linux): More flexible than cron, with better logging via journald:

[Unit]
Description=Daily cleanup job

[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true

[Install]
WantedBy=timers.target

At command for one-time scheduled tasks:

echo "/path/to/script.sh" | at 09:00 tomorrow

Cloud schedulers for serverless:

- AWS EventBridge: Supports both cron and rate expressions (rate(5 minutes))

- Google Cloud Scheduler: Standard cron format, targets Pub/Sub, HTTP, App Engine

- Azure Logic Apps / Functions: GUI-based or cron-driven scheduling

Application-level schedulers:

- Node.js: node-cron, Bull, Agenda

- Python: APScheduler, Celery Beat, rq-scheduler

- Java: Quartz Scheduler, Spring @Scheduled

Cron expressions are compact but precise — a small mistake can cause a critical job to run at the wrong time, too frequently, or not at all. Understanding the five (or six) fields, special characters, and the differences between cron implementations will save you from mysterious production incidents. Use our free Cron Parser tool to translate any cron expression into plain English, see upcoming execution times, and validate your expressions before deploying them — all in your browser, no server required.