Home > Enterprise >  Install Jenkins as Windows Service when I already have existing Jenkins install?
Install Jenkins as Windows Service when I already have existing Jenkins install?

Time:09-17

We have an existing Jenkins install that I run from the command line. I want to start using it as a Windows Service instead, so that it launches when the machine restarts, without requiring someone to log in.

I have read about how to do it, but I am worried that it might break our existing setup, the jobs and other scripts that rely on the current location. Apparently when you go to Install Jenkins as a Windows Service, it asks you for a location for JENKINS_HOME.

Can I just give it the existing location? Will it just work or is there a danger of it wiping out what's there? And if I want to be safe and back up everything just in case, can I just make a copy of the existing .jenkins folder and then copy it back if something goes wrong? Or are there other files somewhere that I need to back up?

My question is basically the same as this one, which never got an answer: Install as a Windows Service icon

Click on it and you arrive at ${JENKINS_URL}/install. Point it at your existing install and click "Install". You will get a prompt to restart as a service and then it restarts.
Install tab

You're done. You should see in your logs the system restarting messages:

2021-09-10 00:25:44.077 0000 [id=96]    INFO    jenkins.model.Jenkins#cleanUp: Stopping Jenkins
2021-09-10 00:25:44.080 0000 [id=96]    INFO    jenkins.model.Jenkins$18#onAttained: Started termination
2021-09-10 00:25:44.099 0000 [id=96]    INFO    jenkins.model.Jenkins$18#onAttained: Completed termination
2021-09-10 00:25:44.100 0000 [id=96]    INFO    jenkins.model.Jenkins#_cleanUpDisconnectComputers: Starting node disconnection
2021-09-10 00:25:44.115 0000 [id=96]    INFO    jenkins.model.Jenkins#_cleanUpShutdownPluginManager: Stopping plugin manager
2021-09-10 00:25:44.115 0000 [id=96]    INFO    jenkins.model.Jenkins#_cleanUpPersistQueue: Persisting build queue
2021-09-10 00:25:44.127 0000 [id=96]    INFO    jenkins.model.Jenkins#_cleanUpAwaitDisconnects: Waiting for node disconnection completion
2021-09-10 00:25:44.127 0000 [id=96]    INFO    jenkins.model.Jenkins#cleanUp: Jenkins stopped
[.jenkins] $ C:\Users\         \.jenkins\jenkins.exe start
2021-09-09 17:25:45,153 INFO  - Starting the service with id 'jenkins'

You should also now see the jenkins service running in Windows Services: Jenkins Service

You can manage it via the Services UI, the command line via Services UI - jenkins

NOTE: The same security caveats regarding running as LocalSystem apply regardless of if using this mechanism or the MSI install. Recommend changing to run as a local user; needs LogonAsService permission (Using the LocalSystem Account as a Service Logon Account, Why running a service as Local System is bad on windows). Local Security Policy > Local Policies > User Rights Management > Log on as a service.

C:\>sc query jenkins

SERVICE_NAME: jenkins
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

C:\> sc stop jenkins

SERVICE_NAME: jenkins
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 3  STOP_PENDING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0
          
C:\> sc delete jenkins
[SC] DeleteService SUCCESS

C:\>

C:\Users\          \.jenkins> jenkins.exe /?
A wrapper binary that can be used to host executables as Windows services

Usage: winsw [/redirect file] <command> [<args>]
       Missing arguments trigger the service mode

Available commands:
  install     install the service to Windows Service Controller
  uninstall   uninstall the service
  start       start the service (must be installed before)
  stop        stop the service
  stopwait    stop the service and wait until it's actually stopped
  restart     restart the service
  restart!    self-restart (can be called from child processes)
  status      check the current status of the service
  test        check if the service can be started and then stopped
  testwait    starts the service and waits until a key is pressed then stops the service
  version     print the version info
  help        print the help info (aliases: -h,--help,-?,/?)

Extra options:
  /redirect   redirect the wrapper's STDOUT and STDERR to the specified file

WinSW 2.9.0.0
More info: https://github.com/kohsuke/winsw
Bug tracker: https://github.com/kohsuke/winsw/issues

Images captured from 2.303.1 on Win 10 Enterprise; YMMV.

  • Related