SpunkArt
Command line and project setup

How to read and write a cron expression

A cron expression is five fields that tell a scheduler when to run a job. Each field is a small pattern, and once you can read one field you can read them all. Type an expression below to see what it means and when it fires next.

Cron expression explainer

The five fields, left to right

PositionFieldAllowed values
1Minute0–59
2Hour0–23 (24-hour clock)
3Day of month1–31
4Month1–12 or jan–dec
5Day of week0–7 or sun–sat (0 and 7 are both Sunday)

A handy way to remember the order is that it runs from the smallest unit of time to the largest, then adds the weekday at the end. The job fires in every minute where all the fields agree with the clock.

Four symbols do all the work

  • * means every value of that field.
  • , separates a list: 0,30 in the minute field means on the hour and at half past.
  • - makes an inclusive range: 1-5 in the weekday field is Monday to Friday.
  • / adds a step: */15 in the minute field is 0, 15, 30 and 45. A range can take a step too, so 9-17/2 in the hour field is 9, 11, 13, 15 and 17.

Reading an expression step by step

Example: 30 2 1 * *

  1. Minute is 30.
  2. Hour is 2, so the time is 02:30.
  3. Day of month is 1.
  4. Month is *, so every month.
  5. Weekday is *, so any day of the week.

Read together: at 02:30 on the first day of every month.

Writing goes in the opposite direction. Start from a sentence such as “every weekday at 6:45 in the evening”. The minute is 45, the hour is 18, the day of month and month are open, and the weekday range is 1–5. That gives 45 18 * * 1-5.

Patterns worth knowing

ExpressionMeaning
*/15 * * * *Every 15 minutes, on :00, :15, :30 and :45
0 */6 * * *At 00:00, 06:00, 12:00 and 18:00
0 9 * * 1-509:00 Monday to Friday
0 0 * * 0Midnight at the start of Sunday
0 0 1 1 *Midnight on 1 January
@dailyShortcut for 0 0 * * *

The day-of-month and weekday trap

When both day fields hold a real restriction, classic cron runs the job when either one matches, not both. So 0 0 13 * 5 does not mean “Friday the 13th”. It means every 13th of the month and every Friday. If either day field starts with *, the two must both match, which is why the usual patterns behave as you expect. To target one weekday in a date range, schedule the weekday and let the script check the date itself.

Common mistakes

  • Putting the hour first. 9 30 * * * is invalid because the hour field only goes to 23. Minute always comes first.
  • Writing * 9 * * * for “at 9”. The star in the minute field makes it run sixty times, from 09:00 to 09:59.
  • Uneven steps. */7 in the minute field gives 0, 7, … 56 and then 0 again, so the gap from :56 to the next :00 is only four minutes. Steps restart every hour.
  • Forgetting the time zone. Cron uses the clock of the machine it runs on, which is often UTC on servers.

Questions

Can cron run every 30 seconds?

Not directly; the smallest unit is one minute. A common workaround is two entries, one of which sleeps 30 seconds before it starts.

Do other tools use the same syntax?

Many do, but not all. Some schedulers add a seconds field at the front or a year at the end, and some support extra symbols such as L or ?. Check the documentation of the tool you use.

How does the explainer above work?

It parses the five fields in your browser and steps forward through the calendar to list the next five matches in your local time zone. Nothing is sent anywhere.