Cron Expressions: Beginner's Guide with Examples
10 min read

Cron Expressions: Beginner's Guide with Examples

Learn how to write cron expressions for scheduling tasks. Includes common patterns and a visual builder tool.

crondevopsschedulingtutorial
Share:

Cron Expressions Explained: A Beginner's Guide with Examples

Master cron syntax and never misconfigure a scheduled job again.


What is Cron?

Cron is a time-based job scheduler in Unix-like operating systems. It runs tasks automatically at specified intervalsβ€”backups at midnight, reports every Monday, cache clearing every hour.

The "cron expression" is the syntax that defines when a job runs. Understanding this syntax is essential for any developer working with scheduled tasks, whether in Linux crontab, cloud functions, or CI/CD pipelines.


Cron Expression Format

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)
β”‚ β”‚ β”‚ β”‚ β”‚
* * * * *

Each field can contain:

  • A specific value: 5
  • A wildcard: * (any value)
  • A range: 1-5
  • A list: 1,3,5
  • A step: */15 (every 15)

Extended Format (6 fields)

Some systems support a sixth field for seconds:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ second (0-59)
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ minute (0-59)
β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ hour (0-23)
β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ day of month (1-31)
β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ month (1-12)
β”‚ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ day of week (0-6)
β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
* * * * * *

Special Characters

* (Asterisk) - Any Value

Matches all possible values for that field.

* * * * *    β†’ Every minute
0 * * * *    β†’ Every hour (at minute 0)
0 0 * * *    β†’ Every day at midnight

, (Comma) - List

Specifies multiple values.

0 0,12 * * *     β†’ At midnight and noon
0 0 * * 1,3,5    β†’ Mondays, Wednesdays, Fridays at midnight
0 0 1,15 * *     β†’ 1st and 15th of each month

- (Hyphen) - Range

Specifies a range of values.

0 9-17 * * *     β†’ Every hour from 9 AM to 5 PM
0 0 * * 1-5      β†’ Weekdays at midnight
30 * 1-7 * *     β†’ First week of month, every hour at :30

/ (Slash) - Step

Specifies intervals.

*/15 * * * *     β†’ Every 15 minutes
0 */2 * * *      β†’ Every 2 hours
0 0 */3 * *      β†’ Every 3 days

Can combine with ranges:

0 9-17/2 * * *   β†’ Every 2 hours between 9 AM and 5 PM

? (Question Mark) - No Specific Value

Used in day-of-month and day-of-week fields to indicate "no specific value." Only supported in some implementations (like Quartz scheduler).

L (Last)

Last day of month or last X day of week.

0 0 L * *        β†’ Last day of every month
0 0 * * 5L       β†’ Last Friday of every month

W (Weekday)

Nearest weekday to a given day.

0 0 15W * *      β†’ Weekday nearest to the 15th

# (Nth)

Nth occurrence of a weekday in a month.

0 0 * * 1#2      β†’ Second Monday of every month
0 0 * * 5#3      β†’ Third Friday of every month

Common Cron Examples

Basic Intervals

ExpressionDescription
* * * * *Every minute
*/5 * * * *Every 5 minutes
*/15 * * * *Every 15 minutes
*/30 * * * *Every 30 minutes
0 * * * *Every hour
0 */2 * * *Every 2 hours
0 */6 * * *Every 6 hours

Daily Jobs

ExpressionDescription
0 0 * * *Daily at midnight
0 6 * * *Daily at 6 AM
0 9 * * *Daily at 9 AM
0 12 * * *Daily at noon
0 18 * * *Daily at 6 PM
0 0,12 * * *Twice daily (midnight, noon)
0 9,12,18 * * *Three times daily

Weekly Jobs

ExpressionDescription
0 0 * * 0Weekly on Sunday at midnight
0 0 * * 1Weekly on Monday at midnight
0 9 * * 1Every Monday at 9 AM
0 0 * * 1-5Weekdays at midnight
0 0 * * 0,6Weekends at midnight
0 9 * * 1,3,5Mon/Wed/Fri at 9 AM

Monthly Jobs

ExpressionDescription
0 0 1 * *First of every month at midnight
0 0 15 * *15th of every month
0 0 1,15 * *1st and 15th of every month
0 9 1 * *First of every month at 9 AM

Yearly Jobs

ExpressionDescription
0 0 1 1 *January 1st at midnight
0 0 25 12 *December 25th at midnight
0 9 1 */3 *First of every quarter at 9 AM

Real-World Use Cases

Database Backups

# Full backup every night at 2 AM
0 2 * * * /scripts/backup-full.sh

# Incremental backup every 4 hours
0 */4 * * * /scripts/backup-incremental.sh

# Weekly backup rotation on Sunday 3 AM
0 3 * * 0 /scripts/backup-rotate.sh

Log Management

# Rotate logs daily at midnight
0 0 * * * /usr/sbin/logrotate /etc/logrotate.conf

# Clean old logs weekly
0 4 * * 0 find /var/log -type f -mtime +30 -delete

# Compress logs older than 7 days
0 1 * * * find /var/log -type f -mtime +7 -name "*.log" -exec gzip {} \;

Application Tasks

# Send daily digest email at 8 AM
0 8 * * * /app/scripts/send-daily-digest.sh

# Generate reports every Monday at 6 AM
0 6 * * 1 /app/scripts/generate-weekly-report.sh

# Clear expired sessions every hour
0 * * * * /app/scripts/clear-expired-sessions.sh

# Sync data from external API every 15 minutes
*/15 * * * * /app/scripts/sync-external-data.sh

Monitoring & Alerts

# Check disk space every 5 minutes
*/5 * * * * /monitoring/check-disk-space.sh

# Health check every minute
* * * * * /monitoring/health-check.sh

# Generate daily monitoring report
0 7 * * * /monitoring/daily-summary.sh

Cache Management

# Clear application cache daily
0 3 * * * /app/scripts/clear-cache.sh

# Warm cache after clearing
5 3 * * * /app/scripts/warm-cache.sh

# Clear CDN cache weekly
0 4 * * 0 /scripts/purge-cdn.sh

Cron in Different Environments

Linux Crontab

Edit crontab for current user:

crontab -e

View current crontab:

crontab -l

System-wide cron in /etc/crontab includes a user field:

# minute hour day month dow user command
0 * * * * root /scripts/hourly.sh

AWS CloudWatch Events / EventBridge

Uses 6 fields (includes year):

cron(0 12 * * ? *)        # Every day at noon UTC
cron(0/15 * * * ? *)      # Every 15 minutes
cron(0 18 ? * MON-FRI *)  # 6 PM weekdays

Note: AWS uses ? for "no specific value" in day fields.

GitHub Actions

on:
  schedule:
    - cron: '0 0 * * *'    # Daily at midnight UTC
    - cron: '0 */6 * * *'  # Every 6 hours

Kubernetes CronJob

apiVersion: batch/v1
kind: CronJob
metadata:
  name: daily-backup
spec:
  schedule: "0 2 * * *"    # 2 AM daily
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: backup
            image: backup-tool
          restartPolicy: OnFailure

Node.js (node-cron)

const cron = require('node-cron');

// Every day at midnight
cron.schedule('0 0 * * *', () => {
  console.log('Running daily task');
});

// Every 5 minutes
cron.schedule('*/5 * * * *', () => {
  console.log('Running every 5 minutes');
});

Common Mistakes

1. Forgetting UTC vs Local Time

Cron typically uses the system timezone. Cloud services often use UTC. A job scheduled for "9 AM" might run at 4 AM local time.

Fix: Always specify the timezone explicitly when possible, or document what timezone the cron is using.

2. Day of Month AND Day of Week

When both day-of-month and day-of-week are specified, most cron implementations treat them as OR (runs if either matches), not AND.

# This runs on day 1 OR on Mondays, not day 1 that's also a Monday
0 0 1 * 1

3. Overlapping Executions

If a job takes 10 minutes but runs every 5 minutes, you'll have overlapping executions.

Fix: Use flock or similar to prevent concurrent runs:

*/5 * * * * flock -n /tmp/job.lock /scripts/slow-job.sh

4. Missing Output Handling

Cron emails output by default (if mail is configured). This can fill mailboxes or cause jobs to fail silently.

Fix: Redirect output explicitly:

0 * * * * /scripts/job.sh >> /var/log/job.log 2>&1

5. Path Issues

Cron runs with a minimal environment. Scripts that work in your shell may fail in cron because PATH is different.

Fix: Use absolute paths:

# Bad
0 * * * * myscript.sh

# Good
0 * * * * /home/user/scripts/myscript.sh

Or set PATH at the top of your crontab:

PATH=/usr/local/bin:/usr/bin:/bin
0 * * * * myscript.sh

Testing Cron Expressions

Before deploying a cron job:

  1. Validate the syntax - Use a tool to confirm the expression is valid
  2. Check next runs - Verify when the job will actually execute
  3. Test manually - Run the command by hand to ensure it works
  4. Monitor first runs - Watch logs to confirm execution

Try it yourself: Use our Cron Expression Builder to create and test cron expressions visually. See human-readable explanations and preview the next execution times.


Quick Reference Card

Fields

FieldValuesWildcards
Minute0-59* , - /
Hour0-23* , - /
Day of Month1-31* , - / L W
Month1-12 or JAN-DEC* , - /
Day of Week0-6 or SUN-SAT* , - / L #

Common Patterns

PatternMeaning
*Every
*/nEvery n
nAt exactly n
n,mAt n and m
n-mFrom n through m
n-m/sEvery s from n through m

Try It Yourself

Building cron expressions by hand is error-prone. Use our visual Cron Expression Builder to:

  • Build expressions with a simple GUI
  • See human-readable explanations
  • Preview the next 5 execution times
  • Copy expressions with one click

Never deploy a misconfigured cron job again.


Last updated: January 2026

Related Tools

Related Articles