How to Start, Stop or Restart your Instance?

Issue

  • How do I start Jenkins?
  • How do I stop Jenkins?
  • How do I restart Jenkins?

Environment

  • CloudBees Jenkins Platform
  • CloudBees Jenkins Enterprise
  • CloudBees Jenkins Operations Center

Resolution

Administration from Jenkins

The easiest way to stop/restart a Jenkins instance is through the instance itself. In addition to the basic stop and restart commands Jenkins provides a set of commands to safely stop/restart Jenkins by putting it in a quiet mode.

  • quietDown: Put Jenkins in a Quiet mode, in preparation for a restart. In that mode Jenkins don’t start any build
  • cancelQuietDown: Cancel the effect of the “quiet-down” command
  • safeRestart: Puts Jenkins into the quiet mode, wait for existing builds to be completed, and then restart Jenkins
  • safeExit: Puts Jenkins into the quiet mode, wait for existing builds to be completed, and then shut down Jenkins

These commands obey the security rules defined by the Authorization Strategy set up in Jenkins. Only user(s) with the required permission can use the following commands:

URL

http://<jenkins.server>/restart
http://<jenkins.server>/safeRestart
http://<jenkins.server>/exit
http://<jenkins.server>/safeExit
http://<jenkins.server>/quietDown
http://<jenkins.server>/cancelQuietDown

Remote API

URL can be invoked by a Remote API.

Using wget:

$ wget --user=<user> --password=<password> http://<jenkins.server>/restart
$ wget --user=<user> --password=<password> http://<jenkins.server>/safeRestart
$ wget --user=<user> --password=<password> http://<jenkins.server>/exit
$ wget --user=<user> --password=<password> http://<jenkins.server>/safeExit
$ wget --user=<user> --password=<password> http://<jenkins.server>/quietDown
$ wget --user=<user> --password=<password> http://<jenkins.server>/cancelQuietDown

Using cURL:

$ curl -X POST -u <user>:<password> http://<jenkins.server>/restart
$ curl -X POST -u <user>:<password> http://<jenkins.server>/safeRestart
$ curl -X POST -u <user>:<password> http://<jenkins.server>/exit
$ curl -X POST -u <user>:<password> http://<jenkins.server>/safeExit
$ curl -X POST -u <user>:<password> http://<jenkins.server>/quietDown
$ curl -X POST -u <user>:<password> http://<jenkins.server>/cancelQuietDown

Jenkins CLI:

$ java -jar jenkins-cli.jar -s http://<jenkins-server>/ restart
$ java -jar jenkins-cli.jar -s http://<jenkins-server>/ safe-restart
$ java -jar jenkins-cli.jar -s http://<jenkins-server>/ shutdown
$ java -jar jenkins-cli.jar -s http://<jenkins-server>/ safe-shutdown
$ java -jar jenkins-cli.jar -s http://<jenkins-server>/ quiet-down
$ java -jar jenkins-cli.jar -s http://<jenkins-server>/ cancel-quiet-down

System Administration

It is obviously possible to administrate the Jenkins process itself. This is the case, for example, when a machine needs to be restarted or upgraded. It is worth to mention that quiet-down is not available in that case. Only user with the required system permissions should be able to run the following commands.

Unix-based

You installed Jenkins on a Debian-based or a Fedora-based distribution, you can use the following commands:

$ sudo service jenkins restart
$ sudo service jenkins stop
$ sudo service jenkins start

Or in the latest distribution of Linux:

$ sudo systemctl start jenkins.service
$ sudo systemctl stop jenkins.service
$ sudo systemctl restart jenkins.service

IMPORTANT : Do not launch methods start|stop|restart manually as $sudo /etc/init.d/jenkins start|stop|restart because it makes your service unreliable as it picks up the environment from the root user as opposed to a clean reliable blank environment that is set by the init launchers ( service / systemctl).

Windows

You installed Jenkins as a service on Windows, you can either use the UI component Services manager (by running services.msc) or you can use the following command:

$ C:\Program Files (x86)\Jenkins>jenkins.exe start
$ C:\Program Files (x86)\Jenkins>jenkins.exe stop
$ C:\Program Files (x86)\Jenkins>jenkins.exe restart

Mac OS X (Deprecated since CJP 2.7.19)

You installed Jenkins Mac OS, you can use the following command:

$ sudo launchctl unload /Library/LaunchDaemons/org.jenkins-ci.plist
$ sudo launchctl load /Library/LaunchDaemons/org.jenkins-ci.plist

Tomcat

You deployed Jenkins on a Tomcat application server, you can start/stop the application server itself.

Tomcat as a Unix service:

$ service tomcat7 start
$ service tomcat7 stop
$ service tomcat7 restart

Tomcat as a Windows service:

$ <tomcat.home>/bin/Tomcat.exe start
$ <tomcat.home>/bin/Tomcat.exe stop

Tomcat running Mac/Linux/Unix binaries:

$ $CATALINA_HOME/bin/startup.sh
$ $CATALINA_HOME/bin/shutdown.sh

Tomcat running Windows binaries:

$ %CATALINA_HOME%\bin\startup.bat
$ %CATALINA_HOME%\bin\shutdown.bat

However, if several application are deployed on your application server, it is preferable to manage the Jenkins application independently rather than restarting all applications. The following URLs shows how to start/stop/restart an application like jenkins deployed on a Tomcat server.

Tomcat 7+:

http://<tomcat-server>:8080/manager/text/stop?path=/jenkins
http://<tomcat-server>:8080/manager/text/start?path=/jenkins
http://<tomcat-server>:8080/manager/text/reload?path=/jenkins

Stand-Alone

If you are running Jenkins as a stand-alone application (in a Winstone servlet container), you can restart the application using the following command:

$ java -cp $JENKINS_HOME/war/winstone.jar winstone.tools.WinstoneControl reload: --host=localhost --port=8001
$ java -cp $JENKINS_HOME/war/winstone.jar winstone.tools.WinstoneControl shutdown: --host=localhost --port=8001

This only works if you specified the controlPort in the start command:

$ java -DJENKINS_HOME=/path/to/home -jar jenkins.war --controlPort=8001
Have more questions? Submit a request

2 Comments

  • 0
    Avatar
    Ericchou19831101 Ericchou19831101

    I observe a strange behavior.

    I have jenkins running there for a couple of months, never did restart (by systemctl or from Web GUI)

    and I can see the process was start in May.

    Then I did a /restart from Web GUI, the process is still the same one.  ( re-tried twice , same thing) even Web GUI showed "jenkins is restarting...." 

    Then I did a "systemctl restart jenkins", and from Web GUI, it does the restart. and after one minute, I can access from Web GUI again, and on jenkins master, I can see the process is updated to a new one.

     

    So my question is, what exactly /restart does? Definitely it is not the same as a "systemctl restart"

  • 0
    Avatar
    Denys Digtiar

    On *nix systems, the current implementation uses the function from exec suite to restart the process. This has a side-effect of all the process attributes being preserved even though JVM is completely reloaded.

    You can refer to code here - jenkins/UnixLifecycle.java at 08a993d7bcd3590ccbfa9504cd45fbba540fd354 · jenkinsci/jenkins

Please sign in to leave a comment.