How do I write this Cron Expression? - grails

I am trying to write a cron expression for a job which should run every 3 days at 9AM GMT. This is what I am writing
"0 0 9/72 ? * ?"
But I get it's as an invalid cron expression because I suppose there can not be two question marks in that. So how should I create a cron job for my use case?
I can not put a * as that would mean the cron must run every day, which it shouldn't.

With grails & quartz-scheduler, the right syntax would be :
"0 0 9 */3 * ?"

0 9 */3 * * <Your Command>
This will run the job every 3 days at 9:00 AM.
Cron Syntax.

Related

"H 5 * * 7" cron in Jenkins groovy script

I am trying to understand when this trigger runs in Jenkins groovy script. Seems to me that Jenkins groovy script has some DSL for cron. Does anyone know when this cron job is triggered?
triggers {
cron('H 5 * * 7')
}
In H 5 * * 7 the H means hash. You can have digit in the first place, but H allows to distribute the load better.
H = minute when less load is expected, Jenkins will propose it the first time, after that it will stay more or less the same, if there are available agents to handle it.
5 = hour,
* = any value (every day),
* = any value (every month),
7 = 7th day of week (Sunday).
Reference:
Spread load evenly by using ‘H * * * *’ rather than ‘5 * * * *’
You can test it using the CRONTab tester.
This means to start at 5:00 on every sunday
https://crontab.guru/#0_5_*_*_7

How to schedule a Jenkins job every 3 hours but exclude a certain time frame

I want to schedule a jenkins job every 3 hours but exclude 6 o'clock. Something like the following statement which unfortunately is not possible in Jenkins:
H (0-5,7-23)/3 * * *
If I use the following statement it is treated separately:
H 0-5/3,7-23/3 * * *
Hence, the job starts at 0 o'clock even if the previous job was executed at 22 o'clock.
The worst solution would be to specify all times manually. Is there any better way?
You will need to explicitly set the hours.
So if you want to skip 6 PM then Jenkins corn will be
H 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,19,20,21,22,23 * * *
and if you want skip 6 AM then
H 0,1,2,3,4,5,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 * * *

Schedule a Jenkins Job to run hourly everyday but skip midnight (00:00) and resume again from 01:00 till 23:00

I have a Jenkins job which I would like to run from (01:00) 1 AM till (23:00) 11PM but skip midnight (00:00) and resume again from (01:00) on a daily basis. The thread How to schedule Jenkins job every Hour for the next 12 hours had this example H 9-21 * * * which i have changed to H 1-23 * * * to cater for my example.
Am I on the right track perhaps with using H 1-23 * * * ??
Yes, that should work. There is a cron tester you can look that will show you the cron version of that, and for Jenkins you just replace the 0 with the H.
http://cron.schlitt.info/index.php?cron=0+1-23+*+*+*+&iterations=50&test=Test

I want Jenkins job to build every two weeks

Will this expression run the build every other Friday at noon? Assume i set this up on a Friday?
0 12 * * */14
I tried 0 12 * * FRI/14 but Jenkins returned an error.
I ma trying to run a code report job every two weeks to match scrum.
You'll have to add some logic to the build script to determine if it ran last week, and then run it every week.
I looked around similar questions for cron jobs, and you have to do some shell magic to make it work.
You could try what was suggested here:
H H 8-14,22-28 * 5
Which would qualify on Fridays that are in the second or fourth week of the month.
it will run at noon every other friday
00 12 */2 * 5
I had the same issue, the easy work around I found was to create another job that run weekly.
This job was a simple groovy script that does the following:
import jenkins.model.*;
def job = Jenkins.instance.getJob('JobNameToRunEveryTwoWeek')
job.setDisabled(!job.isDisabled())
Since Jenkins does not offer the functionnality its the best easy solution I could find. If you have better solution feel free to let me know.
One ridiculous-looking-but-it-works answer: schedule your job to run every week, and then at the top of the job add the following:
// Suppressing even number builds, so this job only runs
// every other week.
def build_number = env.BUILD_NUMBER as int
if ((build_number % 2) == 0) {
echo "Suppressing even number builds!"
echo """THIS IS A HACK TO MAKE THIS JOB RUN BIWEEKLY.
Jenkins cron scheduling currently doesn't support scheduling a
bi-weekly job. We could resort to shell or other tricks to
calculate if the job should be run (e.g., comparing to the date
of the last run job), it's annoying, and this works just as well.
Schedule this job to run weekly. It will exit early every other week.
refs:
* https://stackoverflow.com/questions/33785196/i-want-jenkins-job-to-build-every-two-weeks
* https://issues.jenkins-ci.org/browse/JENKINS-19756
"""
currentBuild.result = 'SUCCESS'
return
}
For Jenkins, you can try this approach as well.
1 1 8-14,21-28 * 5

Jenkins Cron Expression Not Scheduled at Right time

All,
Tried to configure jenkins job to trigger at EVERYDAY 10AM and used below cron H 10 * * * but the jenkins console is not running at 10AM rather its running at 10.09AM. Please help me to run at 10AM everyday around the year.
update: After adding the expression with '0 10 * * *', got below warning and no next run time is displayed. is that normal?
30 5 * * *
will run every day at 5:30 AM
#daily
will run the job once a day at some time, chosen by Jenkins
0 10 * * *
will run every day at 10:00 AM
When not using 'H' in the beginning you will get the warning, you will not get a tip when the job will or would have run, but it will still be active, i.e. it will run as per the statement.
You will always see a syntax error in red color when making any syntax errors.
Also, a good idea might be to create a dummy job to experiment with cron trigger, if you don't feel comfortable with it yet. Or use crontab on Linux:
crontab -e
man crontab
If you really want it at 10AM, please use 0 10 * * *
or 5:30 AM as you asked in comment
30 5 * * *
Jenkins will warn you in this case. If every job schedules like this, load suddenly goes up. Jenkins advice you to differ job time a bit. The H indicate once in every hour, not particularly at 0th minute.

Resources