Jenkins

Jenkins Cron

Cron is a command-line utility that allows the developers and system administrators to schedule the tasks that run in the background at regular intervals.

One major use of cron is automating tasks such as system maintenance, system backups and cleanups, sending automated emails, checking for running services, and more. We can define the cron tasks in the crontab configuration file. This file specifies the tasks that are executed and their respective intervals.

Cron then runs the defined tasks in the background as daemon processes so they can be executed without user interaction.

We can use the cron syntax to schedule the Jenkins jobs. This allows us to define the durations under which the jobs run using the crontab-like syntax.

This fundamentals tutorial covers how the Jenkins crontab syntax works and looks at the practical examples of how to use them. It is good to keep in mind that this tutorial is not meant as an introduction to Jenkins or crontab.

Jenkins Cron Syntax

The Jenkins cron syntax contains five main fields which are separated by a tab or whitespace characters. Each field represents a specific unit of time as shown in the following table:

Jenkins also allows you to specify multiple values for a single field using the following operators:

  1. The * specifies all the valid values.
  2. M-N defines a range of values.
  3. M-N/X or */X – defines the steps by intervals of x through the specified range.
  4. A,B,…Z – enumerates multiple values.

An empty line that starts with a # symbol is treated as a comment and will be ignored.

You can also use aliases such as @yearly, @annually, @monthly, @weekly, @daily, @midnight, and @hourly.

The @hourly alias denotes the h * * * * format.

Example Usage

The following examples demonstrate the various Jenkins cron formats and what each of them represents.

Consider the following example:

*/15 * * * *

The previous expression tells Jenkins to run the job every 15 minutes, every hour.

To set up a Jenkins job, use the previously provided cron expression:

  1. Go to the Jenkins dashboard.
  2. Click on “New Item” in the left-hand menu.
  3. Enter a name for the job and select “Freestyle project” as the type.
  4. Scroll down to the “Build Triggers” section and check the box next to “Build periodically”.
  5. In the “Schedule” field, enter the previous cron expression: */15 * * * *.
  6. Scroll down to the “Build” section and add the build steps for your job.
  7. Click “Save” to create the job.
  8. You should now have a Jenkins job that runs every 15 minutes.

We can also use a cron expression in a Jenkinsfile as shown in the following example:

pipeline {
    agent any
    schedule('*/15 * * * *') {
        stages {
            stage('Build') {
                steps {
                    echo "I run every 15 minutes"
                }
            }
        }
    }
}

This pipeline runs on the top-level stage block (called “Build” in this example) every 15 minutes.

Note: The cron directive only applies on the top level of a Jenkins pipeline such as stage, steps, etc.

To trigger a build when the cron runs, we can use the trigger block as demonstrated in the following Jenkinsfile:

pipeline {
    agent any
    triggers {
        cron('*/15 * * * *')
    }
    stages {
        stage('Build') {
            steps {
                echo "Building project"
            }
        }
    }
}

This script triggers the build every 15 minutes during the cron.

Conclusion

This basic tutorial demonstrates how to use the Jenkins cron feature to schedule a job that runs on a given interval. Feel free to dive into the docs for a detailed information.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list