How-to's and Support

How to Schedule a Jenkins Job: A Simple Guide

6 min read

Chips and guac. 

Peanut butter and jelly. 

Jenkins and continuous integration/continuous deployment (CI/CD). 

These are all things that naturally go together.

When your team starts using Jenkins, you’ll see how quickly the game changes. Instead of sharing a “build PC” or ensuring everyone uses the same build tools and environment, you’ll only need to share a single system that runs your builds and publishes the results.

But Jenkins is more than just a shared build server. It gives you the ability to respond to events like a commit to source control—and in addition to responding to events, you can also schedule jobs.

In this post, we’ll show you how to schedule Jenkins jobs and we’ll review some specific scenarios. You can also watch CloudBees Developer Advocate Darin Pope walk through a similar tutorial in the following video.

Let’s get started!

Jenkins Triggers

Jenkins and CI/CD

As we mentioned above, Jenkins and CI/CD just make sense together. You can tell Jenkins to look for changes to a branch in Git, and you can also ask it to look for changes in any branch, including new ones. Or, if you’re using a different source control strategy, you can set up your Jenkins jobs to look for new tags.

So, regardless of how your team manages their code, Jenkins can integrate and build changes for you. It can even take your code and perform the next step—deployment—for you, too.

Jenkins and Scheduling

You may not be looking for CI/CD, though. Instead of running a job based on an event, you may want to run it at specific times. 

For example, if an application build needs a significant amount of system resources, you may only want to run it overnight so that your build systems are free for your development teams during the day. Or maybe the opposite is true for you: saving trivial build jobs for after-hours so that your build systems are free for more critical jobs during business hours.

Remember that Jenkins isn’t just for build jobs, either. It can run other tasks for you, too. 

For example, you may use source control to manage configuration data that you want to deploy to production daily or weekly. However, instead of having systems “pull” these configurations from source control with locally scheduled cron jobs, you can use Jenkins to act as a central scheduler. 

How to Schedule a Jenkins Job

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.

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.

Now that we’ve seen some examples, let’s create a job and set it up with a schedule.

Preparing for This Tutorial

If you want to follow along with this tutorial, you’ll need a Jenkins server (we took screenshots from Jenkins LTS version 2.303.3).

Since we’ll only be doing a simple Jenkins job, building on the Jenkins controller node is fine if you do not have the time or resources to configure an agent.

Create a Simple Jenkins Job

Let’s look at how to create a Jenkins job that runs on a schedule. We’ll start with a simple one-step shell job.

Start by creating a new item.

Give your new item a name.

For this example, we’re using a Freestyle project, but the scheduling configuration will be the same for all Jenkins jobs.

Click OK, and Jenkins will create your empty job.

Now, it’s time to give your job something to do. Click on the Build tab on the configure page.

This will take you to the build section. Click on the Add build step menu and select Execute shell.

We’re going to use a one-line shell step for this job.

Enter the single line of shell code and click Save.

Click Build Now. The job will run and complete almost immediately. Next, click the build number to see your results.

Next, click on Console Output, and you’ll see the shell message.

Your job is ready for scheduling!

Add a Schedule to a Jenkins Job

Head back to the job configuration and click the Build Triggers tab.

Now, check the Build periodically box in the Build Triggers section.

This will open the scheduling text area.

Next, let’s set the job to run every five minutes.

Enter H/5 * * * * into the box, click Save, and reopen the configuration. Jenkins will show you when the job will run.

Save the job again and wait a few minutes. Depending on the value Jenkins selects for H, the job will run after a few minutes.

Here is a screenshot of a couple of builds on our system. You can see that the first build corresponds to the time shown above on the configure page.

The timestamps show that you’ve scheduled a job to run every five minutes.

Wait for a bit longer and you will see that it continues to run every five minutes.

Take Advantage of Jenkins Schedules

In this post, we covered Jenkins job scheduling abilities and we covered how to configure jobs for different intervals. To practice what we learned, we created a simple job and scheduled it to run every five minutes.

Jenkins is a valuable infrastructure component that is capable of more than building source code in response to manual requests. Take advantage of its scheduling abilities and elevate your jobs to the next level.

This post was written by Eric Goebelbecker. Eric has worked in the financial markets in New York City for 25 years, developing infrastructure for market data and financial information exchange (FIX) protocol networks. He loves to talk about what makes teams effective (or not so effective!).

Stay up-to-date with the latest insights

Sign up today for the CloudBees newsletter and get our latest and greatest how-to’s and developer insights, product updates and company news!