当我已经安装了现有的 Jenkins 时,将 Jenkins 安装为 Windows 服务?

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

我们有一个现有的 Jenkins 安装,我从命令行 运行。我想开始将它用作 Windows 服务,以便它在机器重新启动时启动,而不需要有人登录。

我已阅读有关如何执行此操作的信息,但我担心它可能会破坏我们现有的设置、作业和其他依赖于当前位置的脚本。显然,当您将 Jenkins 安装为 Windows 服务时,它会要求您提供 JENKINS_HOME.

的位置

我可以只给它现有的位置吗?它会正常工作还是有消灭那里的东西的危险?而且,如果我想确保安全并备份所有内容以防万一,我可以只复制现有的 .jenkins 文件夹,然后在出现问题时将其复制回来吗?还是我需要备份其他文件?

我的问题和这个问题基本一样,一直没有得到答案: Installing existing Jenkins as a Windows Service

谢谢

您应该可以直接从 UI 执行此操作。 (它曾经记录在 Jenkins wiki 上,但是 that's presently down。)

启动命令行 Jenkins java -jar -jenkins.war,转到“管理 Jenkins”(${JENKINS_URL}/manage)。您应该会看到一个图标“安装为 Windows 服务”。

点击它,您将到达 ${JENKINS_URL}/install。将它指向您现有的安装并单击“安装”。您将收到作为服务重新启动的提示,然后它会重新启动。

大功告成。您应该在日志中看到系统重启消息:

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#onAttained: Started termination
2021-09-10 00:25:44.099+0000 [id=96]    INFO    jenkins.model.Jenkins#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'

您现在还应该在 Windows 服务中看到 jenkins 服务 运行:

您可以通过服务 UI、命令行 SC 或 jenkins.exe 二进制文件进行管理:

注意: 关于 运行 作为 LocalSystem 的相同安全警告适用,无论是使用此机制还是 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)。本地安全策略 > 本地策略 > 用户权限管理 > 作为服务登录。

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

在 Win 10 Enterprise 上从 2.303.1 捕获的图像; YMMV.