41

I need to make some configuration changes on our Jenkins instance that will involve restarting Jenkins a couple of times. However, our developers are committing frequently enough that I haven't seen Jenkins without jobs running in three days.

Is there a native way (either through the GUI or via command line) to safe-restart Jenkins? IE: wait for current jobs to finish before going down, and keep track of queued jobs to start once Jenkins comes back up.

I know there's a plugin but in order to install it I need to restart Jenkins...

4
  • Guess from the top of my head, but isn't disabling the slaves an option ? (as far as I remember it doesn't stop ongoing jobs but prevent additional jobs in the queue to be launched)
    – Tensibai
    Commented Jan 17, 2018 at 16:47
  • In theory, with a properly configured Jenkins instance running Pipeline jobs, you can restart the master or slave nodes whenever you would like. My experience says otherwise, however.
    – jayhendren
    Commented Jan 18, 2018 at 18:58
  • 1
    Jenkins is designed to not be affected by restarts. From my experience, it's possible that a build might fail due to a restart, but they are rare. Where I work we used to have to restart Jenkins regularly. We have every kind of job you can imagine, some small, some huge. Only once did we see a build fail where a pickle was not being rehydrated. After that, we always do safe restarts now.
    – Gi0rgi0s
    Commented Feb 20, 2019 at 23:46
  • "a pickle was not being rehydrated" o_O
    – Cody
    Commented Nov 19, 2021 at 19:43

7 Answers 7

23

If you navigate to $YOUR_JENKINS_URL/updateCenter/ you should see the following page:

Here you can check Restart Jenkins when installation is complete and no jobs are running which should be fairly safe.

7
  • It's my understanding that restart only happens if plugins are in the process of installing... there's no way to force that restart to happen without installing plugins, is there?
    – Alex
    Commented Jan 17, 2018 at 16:36
  • 2
    @Alex I just tested it on my instance and for me it seems to always work, regardless of plugin installation. According to stackoverflow.com/a/8077830 there should also be a /safeRestart and /restart endpoints.
    – eyalzek
    Commented Jan 17, 2018 at 17:01
  • 1
    But isn't the question about restarting while jobs are running? Commented Jan 17, 2018 at 17:56
  • @DanCornilescu the question states:IE: wait for current jobs to finish before going down, and keep track of queued jobs to start once Jenkins comes back up., this is exactly what happens when you check that box.
    – eyalzek
    Commented Jan 18, 2018 at 17:09
  • I was asking as the question also states Jenkins without jobs running in three days - which means there will be an impact as some jobs will be delayed. But still, better than losing jobs. Commented Jan 18, 2018 at 17:47
35

Visiting https://youjenkinsdomain/safeRestart will set it in the mode when it waits for jobs to stop and then restarts.

1
  • 2
    I needed to use this specific answer when our Jenkins server bugged out and failed to restart when a plugin required it. Commented Jan 9, 2019 at 2:30
12

You can execute the safeRestart command using either the Jenkins Rest API ([jenkins_url]/safeRestart) or you can execute the command via the Jenkins CLI.

sudo /etc/init.d/jenkins safeRestart

Running a CLI command

The general syntax is as follows (the design is similar to tools like svn/git):

java -jar jenkins-cli.jar [-s JENKINS_URL] command [options...] [arguments...]

JENKINS_URL can be specified via the environment variable $JENKINS_URL. This environment variable is automatically set when Jenkins fork a process during builds, which allows you to use Jenkins CLI from inside the build without explicitly configuring the URL.

NOTE: When running the safeRestart command, any jobs set to be executed during the restart will be queued up and executed when the server is back online. Make sure this does not cause any conflicts upon reboot!

2
  • On my (Debian) system, /etc/init.d/jenkins does not have a "safeRestart" option. That startup script isn't related to either the Rest API or the Jenkins CLI Commented Aug 9, 2021 at 15:51
  • on Ubuntu 20.04LTS updated/upgraded to today's date, jenkins version 2.440.1 rejects the safeRestart argument. This is the help message given in response is: Usage: /etc/init.d/jenkins {start|stop|status|restart|try-restart|force-reload}. I used force-reload successfully. Commented Feb 27 at 18:30
5

To restart Jenkins manually, you can use either of the following commands (by entering their URL in a browser):

(jenkins_url)/safeRestart - Allows all running jobs to complete. New jobs will remain in the queue to run after the restart is complete.

(jenkins_url)/restart - Forces a restart without waiting for builds to complete.

2

I think this is a real problem that many are trying to solve. There are at times where it is preferable to restart Jenkins upon plugin installation. One of the suggestions that I can provide based on my experience is to:

  • Restrict access to the admin module so that not every developer can install plugins
  • Instruct developers on the downtime and inform them ahead of time
  • Install all the required plugins in bulk and restart Jenkins
  • Inform the developers that Jenkins is ready for use

I understand that this requires a manual effort and coordination. But this has worked so far so well in our project, thought no harm in mentioning.

2

Navigate to the Plugin Manager, then click on the Installed tab and scroll to the bottom. There should be a button there that says something along the lines of Restart Jenkins once no jobs are running.

1

Visiting "https://jenkins/safeRestart" does not wait for "jobs/builds" to be finished completely in case of pipeline jobs.

It seems to wait for steps to finish and perform jenkins restart and resume the pipline after jenkins have came up again.

1
  • 3
    I guess this is meant as a comment to the answer of @Nakilon?
    – hagello
    Commented Jun 28, 2018 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.