31

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...

2
  • 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 Jan 17 '18 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 Jan 18 '18 at 18:58
20

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.

6
  • 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 Jan 17 '18 at 16:36
  • 1
    @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 Jan 17 '18 at 17:01
  • 1
    But isn't the question about restarting while jobs are running? – Dan Cornilescu Jan 17 '18 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 Jan 18 '18 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. – Dan Cornilescu Jan 18 '18 at 17:47
27

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. – kayleeFrye_onDeck Jan 9 '19 at 2:30
9

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

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.

1

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.

1

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.

0

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
  • 2
    I guess this is meant as a comment to the answer of @Nakilon? – hagello Jun 28 '18 at 14:08
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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