Skip to content
Engyna

Code & data

Cron Expressions Explained (With Examples)

What the five fields mean, how to read * / , and -, schedules you can copy, and the time zone and day-matching traps that make jobs run at the wrong moment.

Updated · 5 min read

Cron is the scheduler that runs on nearly every Linux server, and its syntax has spread to GitHub Actions, Kubernetes and cloud schedulers. Five short fields decide when your job runs. It's compact and efficient, but it's also easy to get subtly wrong, and you might only notice at 3 am a month later.

The five fields

┌───────── minute (0–59)
│ ┌─────── hour (0–23)
│ │ ┌───── day of month (1–31)
│ │ │ ┌─── month (1–12)
│ │ │ │ ┌─ day of week (0–6, Sunday is 0)
│ │ │ │ │
0 9 * * 1

This expression means "at 09:00 every Monday". Read it from left to right: minute 0, hour 9, any day of the month, any month, and weekday 1.

Many systems also accept 7 for Sunday, as well as names such as MON or JAN.

The special characters

Character Meaning Example Result
* Every value * * * * * Every minute
, A list 0 9,17 * * * At 09:00 and 17:00
- A range 0 9 * * 1-5 09:00, Monday to Friday
/ Steps */15 * * * * Every 15 minutes

You can combine these. 0 9-17/2 * * 1-5 runs at 9, 11, 13, 15 and 17 o'clock on weekdays.

Common schedules to copy

Schedule Expression
Every 5 minutes */5 * * * *
Every hour, on the hour 0 * * * *
Every day at midnight 0 0 * * *
Every day at 02:30 30 2 * * *
Weekdays at 09:00 0 9 * * 1-5
Every Sunday at 03:00 0 3 * * 0
First day of the month at 06:00 0 6 1 * *
Every three months 0 0 1 */3 *
Once a year, 1 January 0 0 1 1 *

Many implementations offer shortcuts too, such as @hourly, @daily, @weekly, @monthly, and @reboot.

To get a schedule without doing this by hand, use the cron expression generator. It also explains expressions in plain English, and lists the next run times.

The most common mistakes

Forgetting the minute

* 9 * * * looks like "at 9 am", but it runs every minute from 09:00 to 09:59, or 60 times. What you wanted is 0 9 * * *.

Day of month and day of week together

If both fields are restricted, standard cron runs the job when either of them matches, not when both do.

0 9 13 * 5 doesn't mean "Friday the 13th". It means "the 13th of every month, and every Friday". To run only on Friday the 13th, schedule the job for Fridays, and add a date check in the command.

Step values don't count from the start time

*/40 * * * * doesn't run every 40 minutes. It matches minutes 0 and 40 within each hour, so the gap alternates between 40 and 20 minutes. Steps restart each hour, or each day, or whatever the field is. For an even interval that doesn't divide cleanly into 60, you'll need a different approach.

Assuming a 31st

0 0 31 * * runs only in months with 31 days, so it's skipped in the other five. For the last day of the month, some schedulers offer an L value. Otherwise, run it daily from the 28th to the 31st, and check in the script whether tomorrow is the 1st.

Time zones and daylight saving

Cron usually uses the system time of the server, and that's often UTC. A job set for 09:00 on a UTC server will run at a different local time, and the offset can shift twice a year.

If a server is on local time, daylight saving can cause two problems:

  • In spring, the clock skips an hour. A job set inside the skipped hour, such as 02:30, may not run at all.
  • In autumn, an hour repeats. A job in that hour may run twice.

The safest options are to run servers on UTC, or to avoid scheduling between about 01:00 and 03:00. Some schedulers let you set a time zone for each job. GitHub Actions schedules use UTC by default, for example.

Debugging a job that doesn't run

When a job works by hand but not from cron, it's nearly always the environment.

The PATH is minimal. Cron doesn't load your shell profile. Use absolute paths, such as /usr/bin/python3.

No output anywhere. By default, output may go to local mail, or be lost. Redirect it to a file:

0 2 * * * /home/me/backup.sh >> /var/log/backup.log 2>&1

Percent signs. In a crontab, an unescaped % is treated as a newline, so date +%F must be written as date +\%F.

Wrong user. A crontab belongs to a user, and that user might not have access to the files.

Script isn't executable. Run chmod +x on it, or call the interpreter directly.

Missing newline. Some versions ignore a final line that doesn't end with a newline.

Good habits for scheduled jobs

  • Spread out your load. Everyone schedules things at midnight. Pick something like 02:17 rather than 02:00.
  • Prevent overlaps. If a job can take longer than its interval, use a lock, for example with flock.
  • Alert on failure. Silent failure is the biggest danger. A backup script that broke months ago is no use when you need it.
  • Make jobs safe to repeat, since retries and time changes can run a job twice.
  • Add a comment above each line explaining what the job does.

Six fields and other dialects

Cron isn't entirely standard. Some systems add a seconds field in front, or a year field at the end. Quartz, used in Java applications, has six or seven fields, and uses ? for "no specific value". Check your scheduler's documentation before you copy an expression. The generator has a switch for a leading seconds field.

For checking when something ran in your logs, the Unix timestamp converter turns epoch values into readable dates.