How do I schedule jobs in Jenkins?

832,230

Solution 1

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

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 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

*/5 * * * * means every 5 minutes

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

Solution 5

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 * *

Share:
832,230
Sangram Anand
Author by

Sangram Anand

Updated on July 08, 2022

Comments

  • Sangram Anand
    Sangram Anand almost 2 years

    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.

  • Sangram Anand
    Sangram Anand over 11 years
    I want to run the job exactly @ 8:00 AM EST so the cron expression i constructed is - 00 08 * * * is that fine ?
  • Sangram Anand
    Sangram Anand over 11 years
    Unfortunately its not working, attached the screen shot of it in the post, btw am testing it with the current EST time i.e., 4:20 AM something..
  • fduff
    fduff over 11 years
    that EST time is the same timezone on the build server?
  • Sangram Anand
    Sangram Anand over 11 years
    yes the time is same , btw the */5 * * * * (every 5mts) works well.
  • ѕтƒ
    ѕтƒ over 10 years
    So how to build periodically after every 6hrs. Does * */6 * * * will work?
  • Cédric Julien
    Cédric Julien over 10 years
    @ѕтƒ : yes, it will launch the build every 6 hours
  • friederbluemle
    friederbluemle about 10 years
    Spread load evenly by using ‘H/5 * * * *’ rather than ‘*/5 * * * *’
  • AndyClaw
    AndyClaw over 9 years
    If you want to limit the range of the H you can use H(1-3) for example to choose a number between 1 and 3. Example would be H H(1-3) * * * to run once between 1am and 3am
  • ajeetdl
    ajeetdl about 9 years
    Frustratingly, there seems to be something in my brain that simply cannot grasp these expressions without constantly having to refer back to docs.
  • AKS
    AKS over 8 years
    To run job exactly at 8am EST, you have to do this way: H(0-0) 8 * * *
  • AKS
    AKS over 8 years
    H(0-0) 6 * * 1 (for running at 6am ET on Monday), H(0-0) 16 * * 0 (At 4pm ET on Sunday). Running plain H 16 (won't run at 1600 / 4pm), you have to use H(0-0) 16 ....
  • Helmut Granda
    Helmut Granda about 8 years
    @SkillM2 Don't forget the words from Albert Einstein - “Never memorize something that you can look up.”
  • derHugo
    derHugo over 6 years
    @ArunSangal instead of H(0-0) you can simply use 0 the advantage of using H is that not all Jobs will attempt to run at the same time exactly but rather try to get "the next available time-slot"
  • ziggy
    ziggy about 5 years
    What is the difference between DAYMONTH and DAYWEEK. How would both be used? For example, what does this mean? 01 23 * * 5
  • Sergey Makhonin
    Sergey Makhonin almost 5 years
    @ziggy this would mean that job will run at 23:01 of every month at friday. The difference is that a month has from 28 to 31 days, while a week has only 7, where 1 is Monday and 7 is Sunday. Note that 0 is also Sunday.
  • Balaji Boggaram Ramanarayan
    Balaji Boggaram Ramanarayan over 4 years
    how to apply these changes ?
  • chrisinmtown
    chrisinmtown over 4 years
    at-midnight is pretty cool :) Jenkins seems to recognize quite a few special values starting @ including @daily. Can you post a link to the definitive list?
  • Gavriel Cohen
    Gavriel Cohen over 4 years
    @chrisinmtown, predefined aliases list added, hopefully, it's helpful for you :)
  • Stunner
    Stunner almost 4 years
    In the 4th example u r starting ur build on saturday night 11:59pm , that is 1 minute before the midnight :) just saying
  • Gunjan Paul
    Gunjan Paul over 3 years
    Thank you. this is very helpful to me to understand the * concept of Jenkins
  • Johan Boulé
    Johan Boulé over 3 years
    Thanks for the explanation about H being related to a hash. I still think that's an unfortunate naming choice by the Jenkins authors though. In this context, they could have guessed that everyone would intuitively associate H with hour, and choose something else.
  • ChandraSPola
    ChandraSPola about 3 years
    When you say 0 and 7 are sunday is that a Typo ? 0 being monday , 7 being sunday , is that what you mean ? @SergeyMakhonin . Thank you for clarifying . I read your comment after asking a question.
  • Zvezda Bre
    Zvezda Bre over 2 years
    I have to set schedule to execute every 2 days at 7 a.m. Can you help me with schedule code ?