Create/Configure Jenkins Cron Job

What are Jenkins cron jobs? Why do you need them?

As the name suggests, "cron job" it allows you to set up a schedule to run pipelines automatically. You don't need to manually click the "Build" button every time. Set a time for this job, and it will run those pipelines at that specific time with the frequency you choose, whether daily, weekly, monthly, or even yearly. So basically, it automatically triggers the pipeline builds without any manual interaction.

When should you use a cron job?

  1. Scheduled Task:

    When you need to run the same script repeatedly at specific times or intervals, such as daily, weekly or monthly.

  2. Monitoring:

    When you are working on some monitoring tasks and need to generate reports that runs frequently, like monitor system resources, logs or network activity.

  3. System Maintenance:

    While performing routine system maintenance, like updating software or running security scans.

  4. Backup & Recovery:

    When you want to automate backups and recovery processes.

Creating a Jenkins Cron Job with an Example

Here, we will be creating a Jenkins cron job for a scheduled task that generates two types of report of “azure cost”:

  1. Daily Report:

    The daily report script(pipeline) should run every day at 2 AM IST. This pipeline runs and generates a daily azure cost report and then stores it on the azure container.

  2. Monthly Report:

    The monthly report script(pipeline) should run on every 6th date of the month at 2:30 AM IST. This pipeline runs and generates a monthly azure cost report and then stores it on the azure container.

So basically, one Jenkins job that has two cron setups.

Step 1: Create a Jenkins job

  1. Click on the job you want to schedule (or create a new one)

  2. Scroll down to the Build Triggers section.

  3. Check the box next to Build periodically with parameters

Step 2: Configure Cron Schedule

  1. In the Schedule field, enter your desired cron expression (e.g., * * * * * for every minute). To understand “cron expressions” in simpler way, you can use this link: Crontab.guru - The cron schedule expression generator

  2. In our case, our Jenkins job is a parametrized job. The parameter “Report_Type” accepts two values “Daily” and “Monthly” for generating reports accordingly.

  3. So now, in the Schedule field we write two cron expression as follows:

    1. For Daily Report(runs pipeline daily at 2am):

      0 2 * %Report_Type=Daily

    2. For Monthly Report(runs pipeline on every 6th at 2:30am):

      30 2 6 %Report_Type=Monthly

Step 3: Save Changes

Declarative Pipeline Configuration Example:

The above mentioned cron job can also be set up using a Jenkinsfile by adding these code in it:

Verify Cron Job

  1. Go to the Jenkins dashboard.

  2. Click on the job.

  3. Click on Build History

  4. Verify the scheduled build is triggered at the specified time.

Kudos!! By following the above mentioned steps, you should now have a Jenkins cron job set up and running according to your schedule.

Reference Docs:
Parameterized Scheduler | Jenkins plugin

0
Subscribe to my newsletter

Read articles from Yogendra Singh Rathore directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yogendra Singh Rathore
Yogendra Singh Rathore

Hey tech buddies, Yogendra Here !! I love to learn and share stuff about DevOps. Feel free to connect and explore how my skills can align with your needs.