NadirTools

Cron Syntax Reference & Operator Guide

2 min read

Master the five-field cron system, special characters, and syntax formatting rules.

The Five Fields of standard Cron

A standard Unix cron expression consists of five space-separated fields representing the time interval of execution. The scheduler evaluates these fields continuously to determine if a job should trigger.

* * * * *

+--------------------- Minute (0 - 59)

Special Operators

- `*` (Asterisk): Matches any value in the field. For example, `*` in the hour field means 'run every hour'.

- `,` (Comma): Separates a discrete list of values. `1,5` in the hour field means run exactly at 1:00 AM and 5:00 AM.

- `-` (Hyphen): Defines an inclusive range of values. `1-5` in the day-of-week field means Monday through Friday.

- `/` (Slash): Specifies step increments. `*/15` in the minute field means run every 15 minutes (0, 15, 30, 45).

Common Examples

- `0 0 * * *`: Run once a day at midnight.

- `0 2 * * 1-5`: Run at 2:00 AM every weekday.

- `*/5 * * * *`: Run every 5 minutes.

The 'Day of Month' vs 'Day of Week' Conflict

If both the Day of Month and Day of Week fields are restricted (not `*`), the command will execute when *either* field matches. For example, `0 0 13 * 5` runs at midnight on every Friday AND on the 13th of every month, not just on Friday the 13ths.

Frequently Asked Questions

Q: What does the asterisk (*) mean in cron?

The asterisk is a wildcard that means 'every'. If placed in the minute field, it means the job runs every minute. If placed in the month field, it runs every month.

Q: How do I run a cron job every 15 minutes?

Use the step operator (/) in the minute field: '*/15 * * * *'. This tells the scheduler to execute the job at minute 0, 15, 30, and 45 of every hour.

Q: What day is 0 in a cron expression?

In standard Unix cron, 0 represents Sunday. Interestingly, 7 also represents Sunday, making both valid.