cron schedules

Sample cron schedules

  1. At 00:05 on every day-of-month from 6 through 20 and on every day-of-week from Monday through Friday. Five minutes after the start of every hour.
    5 0 06-20 * * MON-FRI
    
  2. At every minute.
    */1 * * * *
    

Spring cron vs normal cron?

References:

  1. https://stackoverflow.com/questions/30887822/spring-cron-vs-normal-cron
  2. https://docs.spring.io/spring-framework/reference/integration/scheduling.html

Spring Scheduled tasks are not in the same format as cron expressions.

They don’t follow the same format as UNIX cron expressions.

There are only 6 fields:

  1. second,
  2. minute,
  3. hour,
  4. day of month,
  5. month,
  6. day(s) of week.

Asterisk (*) means match any. */X means “every X” (see examples).

Numeric days of the week do not work for me. Besides, “MON-FRI” is much easier to read. Here are some example expressions:

"0 0 18 * * MON-FRI" means every weekday at 6:00 PM.

"0 0 */1 * * *" means every hour on the hour.

"0 0 */8 * * *" means every 8 hours on the hour.

"0 0 12 1 * *" means 12:00 PM on the first day of every month.

A Spring Scheduled tasks is like this:

1 2 3 4 5 6 Index
- - - - - -
* * * * * * command to be executed
- - - - - -
| | | | | |
| | | | | ------- Day of week (MON - SUN)
| | | | --------- Month (1 - 12)
| | | ----------- Day of month (1 - 31)
| |-------------- Hour (0 - 23)
| --------------- Minute (0 - 59)
----------------- Seconds (0 - 59)

A Linux Cron job is like this:

1 2 3 4 5 Index
- - - - -
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

Helpful resources

  1. https://crontab.guru/
  2. https://www.baeldung.com/cron-expressions

Links to this note