Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cron Expression for "After Every 30 days at midnight" ?

 
Karn Kumar
Ranch Hand
Posts: 153
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All ,

Can anybody suggest that how can i run job after every 30 days

I tried with the

0 0 0 1/30 * ? *

on the http://www.cronmaker.com/


but it shows some output like


1. Monday, October 31, 2011 12:00 AM
2. Tuesday, November 1, 2011 12:00 AM ---- (This is immediate day after 31)
3. Thursday, December 1, 2011 12:00 AM
4. Saturday, December 31, 2011 12:00 AM
5. Sunday, January 1, 2012 12:00 AM

Can anybody suggest way or any alternate way ...


Thanks in advance..

Regards,
Chetan
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've never seen a slash in a crontab entry. I'm surprised that's not an error.

But...I don't think you can schedule a cron job to run every 30 days. You can pick a day of week or day of the month...but every 30 days requires something to remember the last time it ran. I don't think cron does that.
 
Karn Kumar
Ranch Hand
Posts: 153
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Slash Can be used and is mentioned in the class CronExpression

http://quartz-scheduler.org/api/1.8.5/org/quartz/CronExpression.html

But now the problem is if it runs on 31Dec then it will again run on 1 Jan which should not happen it should run 30 days after 31 Dec say 30 Jan .......
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't know there was such a class. If you read the api carefully, "1/30" means "run it on the 1st and 30 days after the first" - NOT 'run every 30 days'. I still don't see how to schedule it like you want. It doesn't work that way.
 
Karn Kumar
Ranch Hand
Posts: 153
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi fred ,

You are right that it will run on 1 st and 30 days after that , but i want to run it after 30 days interval....


Thanks...!!


 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know of a way to do it with cron. You may be able to do it by having a dummy file. When you run, you first check to see if the file is 30 days old or not. If not, shut down. If it is, run the job, then touch the file.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a specifically Java-related question: moving.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic