How to force Jenkins to restart (5 approaches)

Updated: February 3, 2024 By: Guest Contributor Post a comment

Introduction

Restarting Jenkins is a critical task that any Jenkins administrator might need to perform at one point or another. This could be due to plugin installations, updates, or to refresh the state of your Jenkins instance. This tutorial will guide you through several methods to force a restart in Jenkins, ranging from basic to advanced techniques. Knowing multiple approaches to restart Jenkins can be invaluable, especially when working in environments with different configurations and restrictions.

Preparation

Before attempting to restart Jenkins, ensure you have a backup of your instance, including job configurations, plugins, and system settings. This is crucial for restoring your setup in case of failure. Additionally, inform your team about the scheduled restart to minimize workflow disruptions.

Method 1: Using the Jenkins Web Interface

The simplest way to restart Jenkins is through its web interface. This method is straightforward and recommended for most users.

  1. Navigate to http://yourjenkinsserver/safeRestart. This URL forces Jenkins into a safe restart, ensuring that no builds are running.
  2. You will be prompted to wait until the running builds complete or to forcefully interrupt them. Make your choice based on your operational needs.
  3. Once the builds have paused or completed, Jenkins will automatically restart.

Output: You will see a screen indicating that Jenkins is restarting. The restart process may take a few minutes, depending on your server’s specifications and load.

Method 2: Restart via the Jenkins CLI

For users who prefer the command line or need to script their restart process, Jenkins provides a Command Line Interface (CLI) tool.

  1. Ensure the Jenkins CLI is set up and you have the necessary permissions to execute commands.
  2. Execute the restart command:
    java -jar jenkins-cli.jar -s http://yourjenkinsserver/ restart
  3. This command initiates a safe restart. Make sure you replace http://yourjenkinsserver/ with the actual URL of your Jenkins instance.

Output: Typically, there is no output from this command; however, you can verify the restart by accessing the Jenkins UI or checking the server logs.

Method 3: Using REST API

For more advanced scenarios, such as integrating Jenkins restarts into automated workflows, the Jenkins REST API can be a powerful tool.

  1. Send a POST request to the Jenkins API to initiate a restart:
    curl -X POST http://yourjenkinsserver/restart -u username:token
    Replace username and token with your Jenkins credentials. Ensure you have the ‘restart’ token generated in your Jenkins management settings.
  2. This method forcibly restarts Jenkins, so use it with caution, especially in production environments.

Output: The command line will return to the prompt after the command executes, indicating the restart command has been sent. Check the Jenkins web interface or log files for the status of the restart.

Method 4: Scripting a Safe Restart

In cases where you need more control over the restart process, writing a script might be the optimal solution. Such a script can check for active builds and wait until they have completed before initiating the restart.

Using Groovy script executed via the Jenkins Script Console (located at http://yourjenkinsserver/script) can allow for a controlled restart.

if (Jenkins.instance.isQuietingDown) {
    println("Jenkins is already in the process of restarting...")
} else {
    Jenkins.instance.doQuietDown()
    println("Jenkins is preparing to restart...")
    sleep(30000) // wait for 30 seconds
    Jenkins.instance.doSafeRestart()
}

Output: This script outputs the current status of Jenkins – whether it’s preparing to restart or already in the process. After executing, it performs a safe restart following a 30-second wait period.

Method 5: Using System Scripts

If you have access to the server where Jenkins is hosted, you might prefer to restart Jenkins by directly controlling the service. The exact command depends on your server’s operating system.

  1. For Linux systems, use:
    systemctl restart jenkins
  2. For Windows servers, use:
    Restart-Service Jenkins

Output: These commands do not produce a direct output in the terminal but restarting the Jenkins service in this manner should log entries in the system’s service management logs.

Conclusion

Restarting Jenkins is a multifaceted process, accommodating various environments and scenarios. Whether you prefer a simple web interface method, the flexibility of the CLI, the power of the REST API, the control of scripting, or direct service management, there’s a technique suitable for your context. Always back up your configuration and notify stakeholders before proceeding to ensure a smooth restart process.