26

I am using Quartz Scheduler to run my jobs. I want to run my job every thirty seconds. What will be my cron expression for that?

For every one minute, I am using below cron expression:

<cron-expression>0 0/1 * 1/1 * ? *</cron-expression>

What it will be for every thirty seconds?

user1950349
  • 4,738
  • 19
  • 67
  • 119

4 Answers4

34

The first element represents the seconds; to run at second 0 and 30 use the following:

<cron-expression>0/30 0/1 * 1/1 * ? *</cron-expression>
Pang
  • 9,564
  • 146
  • 81
  • 122
Ian Mc
  • 5,656
  • 4
  • 18
  • 25
  • 1
    Thanks Ian. One more thing what will be the cron expression to run every 15 minutes but for the first time it should run immediately? And then afterwards every 15 minutes? Does this looks right `0 0/15 * 1/1 * ? *`? – user1950349 Feb 05 '16 at 00:34
  • That is the correct cron expression for every 15 minutes. Cron is a rigid structure, and doesn't conform well what you are asking (now, now+15min, etc). That cron will run at minute, 0, 15, 30, 45 .. – Ian Mc Feb 05 '16 at 00:37
  • Maybe you need Triggers, not cron – Ian Mc Feb 05 '16 at 00:42
  • If you're using ```spring [spring-boot]``` make sure to omit last * , otherwise it will throw an error saying can't accept fields other than six. – Badri Paudel Mar 21 '22 at 14:42
11

I hope this answer will help you. Please define the cron expression the below

 0/30 * * * * ? *

And then you go this website and test Cron Expression Generator & Explainer - Quartz.

Jar Yit
  • 955
  • 11
  • 22
9

The same effect we can reach (Quartz Spring) using simpler construction:

0/30 * * * * ? *

The last asterisk we can omit.

0/30 * * * * ?

Quartz scheduler cron trigger documentation 2.x

Dave Moten
  • 11,957
  • 2
  • 40
  • 47
hariprasad
  • 555
  • 11
  • 20
0

If you are using Spring framework, make use of @PostConstruct annotation and then @Scheduled(cron=0 0/15 * 1/1 * ?) to trigger now, now+15min and so on.

g00glen00b
  • 41,995
  • 13
  • 95
  • 133
Shailesh
  • 75
  • 10