Skip to main content
SysAdmin Shell Scripting Essentials

Jenkins Cron — Verified Syntax, Examples & Troubleshooting

jenkins cron is the scheduling syntax used within Jenkins to automate job and pipeline execution using cron-like expressions, including the hash (H) token for distributed load. It supports both UI-based configuration and declarative Pipeline triggers.

pipeline {
    agent any
    triggers {
        cron('H/15 * * * *')   // Every 15 minutes with hash to prevent burst
    }
    stages {
        stage('Build') {
            steps {
                echo 'Scheduled build'
            }
        }
    }
}

Overview and Use Cases

Jenkins cron expressions define when builds trigger in freestyle jobs and Declarative Pipelines. The H token distributes load by hashing the job name into the minute/hour field. Use cron for periodic tasks such as nightly tests, regular backups, or scheduled deployments.

Syntax Reference

Cron schedules in Jenkins follow the standard five‑field cron format with the optional H token. The expression is defined in the Build Triggers section of a freestyle job or inside the triggers { cron('…') } directive in Declarative Pipeline.

// Declarative Pipeline example
pipeline {
    agent any
    triggers {
        cron('H/15 * * * *')   // Every 15 minutes with hash to prevent burst
    }
    stages {
        stage('Build') {
            steps {
                echo 'Scheduled build'
            }
        }
    }
}

Tested on Jenkins 2.303.3 with CloudBees Pipeline Plugin.

Rapid Reference Cheat Sheet

Action CLI Command / Configuration Context Key Syntax Effect
Every 15 minutes triggers { cron(‘H/15 * * * *’) } Declarative Pipeline H/15 Distribute job start across the hour
Every hour at :45 on weekdays triggers { cron(’45 9-16 * * 1-5′) } Freestyle job Exact minute and hour range Run at fixed times; no hash
Every 10 minutes (first half of hour) triggers { cron(‘H(0-29)/10 * * * *’) } Declarative Pipeline H(0-29)/10 Restrict hash to first 30 minutes
Once every 2 hours (9AM–3PM) triggers { cron(’45 9-16 * * 1-5′) } UI / Pipeline Minute fixed, hour range Runs at 9:45, 11:45, …, 15:45
Daily at midnight triggers { cron(‘H H * * *’) } Declarative Pipeline H H Once per day at a random minute/hour to spread load
See also  Windows runas Command Reference: Syntax & Troubleshooting

Advanced Implementation and Parameters

The H Token (Hash)

Jenkins replaces H with a hash based on the job name. This prevents multiple jobs from firing at the same second. Example: H/15 means every 15 minutes but at a minute offset that is unique per job. Use H(0-29)/10 to constrain the hash to within a range.

Timezone Handling

Jenkins cron evaluates schedules in the controller’s system timezone. To run in UTC explicitly, set the Java timezone with -Dorg.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.timezone=UTC or configure the label “Restrict where this project can be run”. There is no per‑job timezone without plugins.

Using Cron with Environment Variables

Environment variables set in the environment directive are available during the cron trigger. For example, to pass a deployment environment:

pipeline {
    agent any
    environment {
        DEPLOY_ENV = 'production'
    }
    triggers {
        cron('H 2 * * *')
    }
    stages {
        stage('Deploy') {
            steps {
                echo "Deploying to ${DEPLOY_ENV}"
            }
        }
    }
}

Poll SCM vs Build Periodically

Trigger Type Schedule Syntax When Build Starts
Build periodically Same cron expression Always runs at scheduled time, regardless of SCM changes
Poll SCM Same cron expression Only runs if there are new commits since last build

Error Resolution and Troubleshooting

Error Code / Symptom Root Cause Remediation Command / Action
Job never runs at expected time H token or timezone mismatch Check H value by viewing job configuration; verify controller time with date on Jenkins master.
“Invalid cron expression” Extra spaces or invalid range (e.g. H(0-30)/15 is correct; H(0-30)/15 * * * *) Use Jenkins cron validator: https://YOUR_JENKINS/job/JOBNAME/cronValidate or check syntax in UI.
Job runs multiple times per interval Same cron in both job config and Pipeline file (double trigger) Remove one of the triggers; prefer declarative Pipeline cron over UI.
Pipeline cron not registered after loading Pipeline definition not saved or Jenkins restart Force reload: curl -X POST http://jenkins:8080/job/JOBNAME/reload or run “Scan Multibranch Pipeline Now”.
“H” token ignored in freestyle job Freestyle job schedules treat H only as a comment; use literal numbers Replace with exact minute, e.g. 0/15 * * * * (but avoid burst).
Job runs even when no changes (Poll SCM) Poll SCM configured with * * * * * – too frequent Change to hour or daily schedule: H * * * *.

Production-Grade Implementation

Scatter Load with Hash

Always use H in production to avoid simultaneous hits on shared infrastructure (e.g., database backups, artifact storage). For example, schedule nightly jobs with H H(0-3) * * * to distribute across the night.

See also  Install wget on macOS — Verified Syntax, Flags & Troubleshooting

Prevent Overlapping Executions

Use the Throttle Concurrent Builds plugin or set options { disableConcurrentBuilds() } in your Pipeline to ensure only one instance runs even if the cron trigger fires while a build is still running.

Secure Cron Credentials

If your cron job executes actions that require secrets, store them in Jenkins Credentials Binding (e.g., withCredentials([string(credentialsId: 'aws-secret', variable: 'AWS_ACCESS_KEY')])). Never embed secrets in triggers { cron } or in script parameters.

Monitor Cron Execution

Enable the Build Log Summary plugin or send notifications via post { success { mail to: '...' } }. For centralized monitoring, expose metrics via the Jenkins Metrics plugin and observe cron‑triggered builds’ duration and success rate.

Frequently Asked Questions

What is the difference between “Poll SCM” and “Build periodically”?

Answer: “Build periodically” triggers builds at fixed intervals regardless of SCM changes. Use “Build periodically” for time-based jobs like nightly tests; use “Poll SCM” for incremental builds when SCM hooks are unavailable. Both accept standard cron syntax. Example:

# Build daily at 03:00
H 3 * * *
# Poll SCM every 5 minutes
H/5 * * * *

When should I use the H (hash) symbol?

Answer: Use H to distribute load across time windows, avoiding thundering herd problems on shared infrastructure. H replaces a fixed number with a pseudorandom value derived from the job name, ensuring consistent offset per job. Example:

# Build at different minutes each hour for each job
H * * * *

How do I fix “Trigger is not firing at the scheduled time”?

Answer: Check the Jenkins controller’s system timezone (configured via JAVA_OPTS or JENKINS_JAVA_OPTIONS) and ensure the cron expression uses valid intervals. Use java -Duser.timezone=UTC to set timezone consistently. Verify through Manage Jenkins > System Information. Debug with:

# Simulate next run times
curl -s "http://jenkins:8080/job/JOBNAME/api/json?tree=nextBuildTime"

Does Jenkins cron work on Windows, Linux, and Kubernetes?

Answer: Yes, Jenkins cron is Java-based and platform-agnostic. No OS-specific differences. For Kubernetes, ensure Jenkins’ home directory persists via PersistentVolume. The cron scheduler runs inside the Jenkins controller process, independent of the host’s cron daemon. Example K8s deployment snippet:

# Jenkins helm chart with PVC
helm install jenkins jenkins/jenkins --set persistence.enabled=true

What is the fastest way to schedule a build every 5 minutes?

Answer: Use H/5 * * * * for load balancing. H/5 spreads the start minute across jobs, reducing contention. To force precise 5-minute intervals (e.g., :00, :05), use fixed expression. Example:

# Distribute: runs at random minute every 5 minutes
H/5 * * * *
# Fixed: runs at 0,5,10,... 
*/5 * * * *