DEV Community

Cover image for Understand cron job expression in 3 minutes ⏰
Max Shen for Repohistory

Posted on • Updated on

Understand cron job expression in 3 minutes ⏰

What is cron jobs?

Cron jobs are scheduled jobs that run periodically at certain times, dates or intervals.

Cron jobs are widely used on different services such as AWS Lambda, Kubernetes, Vercel etc. Even though the platforms are different, the way to configure it is the same, which is through cron expression.

Cron expression

Cron jobs use a specific syntax called a cron expression to determine when a task should run. The syntax consists of five fields separated by spaces:

┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of the month (1 - 31)
│ │ │ ┌───────────── month (1 - 12 or JAN-DEC)
│ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
* * * * *
Enter fullscreen mode Exit fullscreen mode

You can use the following operators in any of the five fields:

* (Any value)

  • 30 2 * * * runs at 02:30 of every day.

, (Value list separator)

  • 0 6,18 * * * runs at 06:00 and 18:00 of every day.

- (Range of values)

  • 0 4-6 * * * runs at 04:00, 05:00, 06:00 of every day.

/ (Step values)

  • 20/15 * * * * runs at minutes 20, 35, and 50 of every hour (15 minutes starting from minute 20 through 59)
  • 0 */8 * * * runs at 00:00, 08:00, 16:00 of every day

Conclusion

In brief, cron job expressions make it easy to set up automatic tasks. With just five fields and a handful of operators, you can tailor timing to your exact needs. I hope this quick guide helps you understand cron expressions, all within the promised 3 minutes!

Top comments (0)