Preventing Jenkins from running jobs after a restart

You don’t always want Jenkins to start running jobs right after it starts. Sadly, starting up in a dormant state isn’t a built-in feature, unless you’re running CloudBees Jenkins Enterprise. However, there’s little a clever script can’t solve.

This is a short post about getting Jenkins to start in quiet mode, so it doesn’t run any builds. After a series of upgrades, we wanted to verify Jenkins was correctly configured. However, we didn’t want builds to start running while we were still busy.

To solve this, I hacked together a simple Groovy script that puts Jenkins in quiet mode. By setting it up as an initialization script, we have ourselves a sleeping Jenkins on startup. I later fleshed out the script so it can also wake up Jenkins after a few minutes:

import jenkins.model.Jenkins 
import hudson.security.ACL

// Go into quiet mode
Jenkins.instance.doQuietDown()

// Wake up after an async wait
Thread.start {
// doCancelQuietDown requires admin privileges
ACL.impersonate(ACL.SYSTEM)

// Sleep 5 minutes, in milliseconds
Thread.sleep(5 * 60 * 1000)
Jenkins.instance.doCancelQuietDown()
}

To set it up as a Jenkins initialization script, save it as $JENKINS_HOME/init.groovy or place it as a *.groovy file in the $JENKINS_HOME/init.groovy.d/ directory.

When Jenkins is restarted, it will now start up in quiet mode. You can tell by the giant red notification at the top of the page stating “Jenkins is about to shut down”. No worries, it isn’t. The message will disappear once quiet mode gets turned off again.

Published: Dec 12, 2017

Updated: Mar 26, 2024

CI/CD