How do I schedule jobs in Jenkins?

JenkinsCron

Jenkins Problem Overview


I added a new job in Jenkins, which I want to schedule periodically.

From Configure job, I am checking the "Build Periodically" checkbox and in the Schedule text field added the expression:

> 15 13 * * *

But it does not run at the scheduled time.

Is it the correct procedure to schedule a job?

Enter image description here

The job should run at 4:20 AM, but it is not running.

Jenkins Solutions


Solution 1 - Jenkins

By setting the schedule period to 15 13 * * * you tell Jenkins to schedule the build every day of every month of every year at the 15th minute of the 13th hour of the day.

Jenkins used a cron expression, and the different fields are:

  1. MINUTES Minutes in one hour (0-59)
  2. HOURS Hours in one day (0-23)
  3. DAYMONTH Day in a month (1-31)
  4. MONTH Month in a year (1-12)
  5. DAYWEEK Day of the week (0-7) where 0 and 7 are sunday

If you want to schedule your build every 5 minutes, this will do the job : */5 * * * *

If you want to schedule your build every day at 8h00, this will do the job : 0 8 * * *

For the past few versions (2014), Jenkins have a new parameter, H (extract from the Jenkins code documentation):

> To allow periodically scheduled tasks to produce even load on the system, the symbol H (for “hash”) should be used wherever possible. > > For example, using 0 0 * * * for a dozen daily jobs will cause a large spike at midnight. In contrast, using H H * * * would still execute each job once a day, but not all at the same time, better using limited resources.

Note also that:

> The H symbol can be thought of as a random value over a range, but it actually is a hash of the job name, not a random function, so that the value remains stable for any given project.

More example of using 'H'

Solution 2 - Jenkins

The format is as follows:

MINUTE (0-59), HOUR (0-23), DAY (1-31), MONTH (1-12), DAY OF THE WEEK (0-6)

The letter H, representing the word Hash can be inserted instead of any of the values. It will calculate the parameter based on the hash code of you project name.

This is so that if you are building several projects on your build machine at the same time, let’s say midnight each day, they do not all start their build execution at the same time. Each project starts its execution at a different minute depending on its hash code.

You can also specify the value to be between numbers, i.e. H(0,30) will return the hash code of the project where the possible hashes are 0-30.

Examples:

  1. Start build daily at 08:30 in the morning, Monday - Friday: 30 08 * * 1-5

  2. Weekday daily build twice a day, at lunchtime 12:00 and midnight 00:00, Sunday to Thursday: 00 0,12 * * 0-4

  3. Start build daily in the late afternoon between 4:00 p.m. - 4:59 p.m. or 16:00 -16:59 depending on the projects hash: H 16 * * 1-5

  4. Start build at midnight: @midnight or start build at midnight, every Saturday: 59 23 * * 6

  5. Every first of every month between 2:00 a.m. - 02:30 a.m.: **H(0,30) 02 01 * ***

Solution 3 - Jenkins

Jenkins lets you set up multiple times, separated by line breaks.

If you need it to build daily at 7 am, along with every Sunday at 4 pm, the below works well.

H 7 * * *

H 16 * * 0

Solution 4 - Jenkins

*/5 * * * * means every 5 minutes

5 * * * * means the 5th minute of every hour

Solution 5 - Jenkins

The steps for schedule jobs in Jenkins:

  1. click on "Configure" of the job requirement
  2. scroll down to "Build Triggers" - subtitle
  3. Click on the checkBox of Build periodically
  4. Add time schedule in the Schedule field, for example: @midnight

enter image description here

Note: under the schedule field, can see the last and the next date-time run.

> Jenkins also supports predefined aliases to schedule build:

@hourly, @daily, @weekly, @monthly, @midnight

@hourly --> Build every hour at the beginning of the hour --> 0 * * * *

@daily, @midnight --> Build every day at midnight --> 0 0 * * *

@weekly --> Build every week at midnight on Sunday morning --> 0 0 * * 0

@monthly --> Build every month at midnight of the first day of the month --> 0 0 1 * *

Solution 6 - Jenkins

To schedule a cron job every 5 minutes, you need to define the cron settings like this:

*/5 * * * *

Solution 7 - Jenkins

Try this.

> 20 4 * * *

Check the below Screenshot

enter image description here

Referred URL - https://www.lenar.io/jenkins-schedule-build-periodically/

Solution 8 - Jenkins

Jenkins Job Scheduling Syntax First, let’s look at the Jenkins job scheduling configuration. It looks a lot like Linux’s cron syntax, but you don’t have to be familiar with command line Linux to figure it out.

A scheduling entry consists of five whitespace-separated fields. You can schedule a job for more than one time by adding more than one entry.

Screenshot Jenkins Schedules Each field can contain an exact value or use a set of special expressions:

The familiar asterisk * indicates all valid values. So, a job that runs every day has a * in the third field.

A dash separates ranges of values. For example, a job that runs every hour from 9:00 a.m. to 5:00 p.m. would have 9-17 in the second field.

Intervals are specified with a slash /. A job that runs every 15 minutes has H/15 in the first field. Note that the H in the first field has a special meaning. If you wanted a job to run every 15 minutes, you could configure it as 0/15, which would make it run at the start of every hour. However, if you configure too many jobs this way, you can overload your Jenkins controller. Ultimately, the H tells Jenkins to pick a minute based on a hash of the job name.

Finally, you can specify multiple values with a comma. So, a job that runs Monday, Wednesday, and Friday would have 1,3,5 in the fifth field.

Jenkins provides a few examples in their help section for scheduling.

> Every fifteen minutes (perhaps at :07, :22, :37, :52): >
> H/15 * * * * > > > > Every ten minutes in the first half of every hour (three times, > perhaps at :04, :14, :24): >
> H(0-29)/10 * * * * > > > > Once every two hours at 45 minutes past the hour starting at 9:45 AM > and finishing at 3:45 PM every weekday: >
> 45 9-16/2 * * 1-5 > > > > Once in every two hour slot between 8 AM and 4 PM every weekday > (perhaps at 9:38 AM, 11:38 AM, 1:38 PM, 3:38 PM): >
> H H(8-15)/2 * * 1-5 > > > > > Once a day on the 1st and 15th of every month except December: >
> H H 1,15 1-11 *

Jenkins also has a set of aliases that makes using common intervals easier.

Screenshot of table Jenkins Aliases

Solution 9 - Jenkins

Try using 0 8 * * *. It should work

Solution 10 - Jenkins

Jenkins uses Cron format on scheduling. You can refer this link for more detailhttps://en.wikipedia.org/wiki/Cron. One more thing, Jenkins provide us a very useful preview. Please take a look on the screenshot. enter image description here

I hope this help. Thanks

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSangram AnandView Question on Stackoverflow
Solution 1 - JenkinsCédric JulienView Answer on Stackoverflow
Solution 2 - JenkinsNagarjuna YalamanchiliView Answer on Stackoverflow
Solution 3 - JenkinsPawan GaitondeView Answer on Stackoverflow
Solution 4 - JenkinsNikul PadhyaView Answer on Stackoverflow
Solution 5 - JenkinsGavriel CohenView Answer on Stackoverflow
Solution 6 - JenkinsfduffView Answer on Stackoverflow
Solution 7 - JenkinsJayakumari ArumughamView Answer on Stackoverflow
Solution 8 - JenkinsnickView Answer on Stackoverflow
Solution 9 - Jenkinsuser1815823View Answer on Stackoverflow
Solution 10 - JenkinsThuong VoView Answer on Stackoverflow