Skip to content

Running Jenkins agent as a service on Windows ​

Objective ​

Run Jenkins agent.jar file in a Windows service. It's not possible to simply register a service with the exact command as a Windows Service requires a special binary.

I'll be using WinSW for this.

Configuration ​

Install the same major version of Java that is running on Jenkins controller instance on the agent. In my case it's OpenJDK 17.0.10 LTS.

Create a directory, c:\jenkins-agent\, to host Jenkins agent.jar, the service configuration file and the wrapper binary.

Write the configuration of the service:

xml
# c:\jenkins-agent\jenkins-agent.xml
<service>
  <id>jenkins-agent</id>
  <name>Jenkins Agent</name>
  <description>This service runs the jenkins agent process</description>
  <executable>"C:\Program Files\jdk-17.0.10+7\bin\java.exe"</executable>
  <arguments>-jar "C:\jenkins-agent\agent.jar" -url http://JENKINS_HOST/ -secret JENKINS_AGENT_SECRET -name "jenkins-worker-1" -workDir "C:\jenkins"</arguments>
  <log mode="roll" />
  <onfailure action="restart" />
    <serviceaccount>
    <domain>JENKINSWORKER1</domain>
    <user>jenkins</user>
    <password>Passw0rd!</password>
    <allowservicelogon>true</allowservicelogon>
  </serviceaccount>
</service>

Make sure to add the user in Security Settings\Local Policies\User Rights Assignment\Log on as a service otherwise the service will not start

Download the latest release from WinSW and rename the binary to jenkins-agent.exe. The name of the file needs to match between the XML and EXE.

Install the service with:

powershell
c:\jenkins-agent\jenkins-agent.exe install

Further operations on the service can be done with

powershell
c:\jenkins-agent\jenkins-agent.exe [start || stop]

Updated at: