'Jenkins agent as Windows service starts and stops

I am attempting to install a Jenkins agent as a Windows 10 service. I install the service with

jenkins-slave.exe install

and have configured the associated xml file, and the service appears to be properly installed. However, when I start the service, it stops immediately.

A jenkins-slave.wrapper.log file is generated, containing

2021-09-09 11:30:53,503 DEBUG - Starting WinSW in the CLI mode
2021-09-09 11:30:54,191 INFO  - Installing the service with id 'JenkinsAgent'
2021-09-09 11:30:54,238 DEBUG - Completed. Exit code is 0
2021-09-09 11:31:02,164 DEBUG - Starting WinSW in the service mode
2021-09-09 11:31:02,211 INFO  - Downloading: https://<jenkins-server>/jnlpJars/slave.jar to C:\jenkins-agent\slave.jar. failOnError=False
2021-09-09 11:31:02,321 INFO  - Skipped downloading unmodified resource 'https://<jenkins-server>/jnlpJars/slave.jar'
2021-09-09 11:31:02,321 INFO  - Starting java.exe -Xrs -jar "C:\jenkins-agent\slave.jar" -jnlpUrl https:/<jenkins-server>/computer/jetdamdev2/jenkins-agent.jnlp -secret <secret> -workDir "C:\jenkins-agent"
2021-09-09 11:31:02,336 DEBUG - Completed. Exit code is 0

This seems to indicate that the java process is started, but exits about 115ms later.

If I copy the java-launch line into a command-line terminal, it successfully connects to the Jenkins master and stays running.

I have been chasing this issue for quite a while now, with no success. Can someone please tell me what I have done wrong with the service and help me fix this?



Solution 1:[1]

I know I am late for the party, but maybe it comes handy for others: I was following this article: https://hayato-iriumi.net/2019/05/23/how-to-install-jenkins-slave-as-windows-service/ I have faced the same problem with WinSW, and ended up wrapping the whole agent startup command line in a batch file, and run the batch from WinSW. So my jenkins-slave.xml looks like this:

<service>
  <id>JenkinsSlave</id>
  <name>Jenkins agent</name>
  <description>This service runs an agent for Jenkins automation server.</description>
  <executable>c:\jenkins\agent.bat</executable>
  <logmode>rotate</logmode>
</service>

And agent.bat like this:

java -jar agent.jar -jnlpUrl http://<jenkins master URL>/jenkins-agent.jnlp -secret <secret> -workDir "c:\jenkins"

For me this solved the problem. I am stuck at an older, 2.319.1 version of Jenkins for legacy reasons.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 household UFO