Cron Expression Guide: Complete Cron Syntax Reference

Master cron expressions with this comprehensive guide covering syntax, special characters, common schedules, and best practices.

Advertisement

What is Cron?

Cron is a time-based job scheduler in Unix-like operating systems. Users can schedule commands or scripts to run automatically at specified intervals — from every minute to once a year. The name "cron" comes from the Greek word "chronos" meaning time.

Cron is fundamental to system administration and DevOps. It powers automated backups, log rotation, database maintenance, report generation, email sending, cache clearing, and countless other recurring tasks. Beyond Unix systems, cron syntax has been adopted by cloud platforms (AWS CloudWatch, Google Cloud Scheduler), CI/CD tools (GitHub Actions, GitLab CI), and application frameworks (Spring, Laravel, Node.js).

The crontab (cron table) file stores the schedule of cron entries. Each user can have their own crontab, and there is a system-wide crontab as well. Understanding cron expression syntax is essential for any developer or system administrator.

Cron Expression Syntax

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

┌───────────── minute (0-59)
│ ┌───────────── hour (0-23)
│ │ ┌───────────── day of month (1-31)
│ │ │ ┌───────────── month (1-12 or JAN-DEC)
│ │ │ │ ┌───────────── day of week (0-6 or SUN-SAT)
│ │ │ │ │
* * * * *
FieldValuesSpecial Characters
Minute0-59* , - /
Hour0-23* , - /
Day of Month1-31* , - / ? L W
Month1-12 or JAN-DEC* , - /
Day of Week0-6 or SUN-SAT* , - / ? L #

Special Characters Explained

*Asterisk (Wildcard)

Matches every possible value. * in hour means every hour (0-23).

,Comma (List)

Specifies a list of values. 1,3,5 in day-of-week means Monday, Wednesday, Friday.

-Hyphen (Range)

Specifies a range. 9-17 in hour means 9 AM through 5 PM.

/Slash (Step)

Specifies increments. */15 in minute means every 15 minutes. 2/3 means starting at 2, every 3rd.

?Question Mark

No specific value (used in day-of-month and day-of-week when the other is specified). Not supported in all implementations.

LLast

Last day of month or last specific weekday. L in day-of-month means last day. 5L means last Friday.

WWeekday

Nearest weekday. 15W means nearest weekday to the 15th of the month.

#Hash

Nth occurrence. 5#2 means second Friday of the month.

Common Cron Schedules

ExpressionDescription
* * * * *Every minute
*/5 * * * *Every 5 minutes
*/15 * * * *Every 15 minutes
0 * * * *Every hour (at minute 0)
0 */2 * * *Every 2 hours
0 0 * * *Every day at midnight
0 6 * * *Every day at 6:00 AM
0 9 * * 1-5Weekdays at 9:00 AM
0 0 * * 0Every Sunday at midnight
0 0 1 * *First day of every month at midnight
0 0 1 1 *January 1st at midnight (yearly)
30 4 1,15 * *4:30 AM on 1st and 15th of every month

Cron in Different Platforms

Linux/Unix Crontab

# Edit crontab
crontab -e

# List cron jobs
crontab -l

# Example: backup database daily at 2 AM
0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

GitHub Actions

on:
  schedule:
    - cron: '0 6 * * 1-5'  # Weekdays at 6 AM UTC

Node.js (node-cron)

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

Cron Best Practices

  • Always specify timezone — Cron runs in the system timezone by default. Use TZ=UTC or set timezone explicitly to avoid confusion.
  • Redirect output — Always redirect stdout and stderr to log files: >> /var/log/job.log 2>&1
  • Use lock files — Prevent overlapping executions with flock: flock -n /tmp/job.lock command
  • Set PATH explicitly — Cron has a minimal PATH. Set it at the top of your crontab or use absolute paths.
  • Test with short intervals first — Before setting a monthly job, test with every-minute to verify it works.
  • Monitor cron jobs — Use monitoring tools to alert when cron jobs fail or do not run.
  • Stagger execution times — Avoid scheduling everything at minute 0 to prevent resource spikes.

Build Cron Expressions Visually

Use our free cron expression generator to build schedules visually with presets, descriptions, and next execution time preview.

Open Cron Generator →
Advertisement