Tuesday, July 3, 2012

Spring scheduled tasks - cron expression format

I was doing some work with scheduled tasks using Spring 3.1, and was having a lot of trouble getting my cron expressions right.  Turns out they don't follow the same format as UNIX cron expressions, though I'm not sure if the Spring ones would work in UNIX.

Anyway, here's what I learned:

  • There are 6 fields: second, minute, hour, day of month, month, day(s) of week.
  • Asterisk (*) means match any.
  • */X means "every X" (see examples).
  • I don't think numeric days of the week worked 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.

No comments:

Post a Comment