javacroncron-taskcronexpressioncrontrigger

cron trigger that starts immediately and then runs after time interval


I have a requirement wherein i a m writing a utility which requires the cron trigger to fire immediately and then after a regular interval of 30 or 40 minutes. Right now my expression is like this 0 0/40 * * * ? but it starts the trigger after 40 minutes of start of application. What should be the expression or a programmatic way to implement the above scenario in java.


Solution

  • Cron doesn't have functionality to run a job every 40 minutes. In fact */40 * * * * will run the job on 40th minute of every hour, and then at the end of 59th minute of every hour and so on. So intervals between the job will be 40 mins, and then 20 mins, and so forth. The reason is that 60 is not dividable by 40.

    40 * * * * will just run the job on 40th minute of every hour (once an hour).

    */30 * * * * on the other hand will indeed run the job every 30 minutes, because 60 mod 30 = 0

    If you truly want to run every job EVERY 40 minutes, you might need to use some more advanced scheduler, such as fcron (http://fcron.free.fr/). That scheduler truly supports that and some other cool features.

    Answering the question about triggering the job immediately, then I would suggest just programming the job on the very next minute, and inside you script maintaining a counter, and allow the script to run only once. After is script is executed, it can intelligently remove itself from the cron.