16

I want to create a jenkins slave using command line. The is a document about setting up jenkins slave as a windows service and I followed the instruction there. I can run javaws http://10.121.33.4:8080/computer/Test-Jenkins-Slave-2/slave-agent.jnlp to connect slave to master but the problem of this is it will get disconnect after the machine is restarted.

I think the best way to do this is to install slave as a service. So, I tried to follow the instruction again on that page. I have jenkins Slave installed as a service and the registry key added like below.

Service's property:
enter image description here

Registry values:
enter image description hereenter image description here

I wonder if I did anything incorrectly? Is the document missing something?

Anonymous
  • 9,366
  • 22
  • 83
  • 133

5 Answers5

27

(Last 2019-09-20) The quickest and most reliable way that I've found to install a Jenkins Worker/Slave as a service is to:

  1. Download the jnlp file from Jenkins' "/slave-agent.jnlp" endpoint
  2. Bring up the "slave-agent" GUI
  3. Select the "Install as a service" option from the GUI

Detailed steps and pictures below.

Note: Don't bother with modifying the registry, the embedded install makes the process quick, easy, and repeatable.

So lets say you've configured a node named "amberboch". On the worker/slave (i.e. "amberboch") machine:

  1. Bring up a browser and enter the Jenkins URL for new node (or click on the node within the "Nodes" page of Jenkins), and create the node within Jenkins as you normally would.
  2. Once the node is created, navigate to the node (Jenkins > Nodes > amberboch) and include "/slave-agent.jnlp" on the end of the browser's URL (e.g. "http://jenkins:8080/computer/computer/amberboch**/slave-agent.jnlp**") to download the jnlp file. Notice "slave-agent.jnlp" is added manually Save the file
  3. Execute the slave-agent.jnlp file with Administrator privileges. (You may have specify Java/javaws to start it with "Java(TM) Web Start Launcher") (Alternatively, you may use a format found in JENKINS-29616 proved by Pau Sabats to create a new agent jar, which should retain connection information: java -jar newAgent.jar -jnlpUrl http://jenkins:8080/computer/amberboch/slave-agent.jnlp -secret xxxxxxxx -workDir "E:\JenkinsClient")
  4. Select the "File" => "Install as a service" option from the slave-agent.jnlp GUI

Once the service has been installed, change the service's "log-on" credentials as needed for your particular installation. You may have to reset permissions or delete directories within the work-space if, whilst running in Administrator mode, a job happened to run and thereby created a work-space sub-folder (as any jobs that had run would have done so as the former administrator-privilaged user, and the regular Jenkins-user profile may not have proper permissions to the old workspace folders and files.

I hope this helps save you time and headaches in managing (imho) the best CI option I've come across.

Best regards, Rob

PS - I found another discussion that may also help: Install Jenkins slave as a Windows service in command line

Robert
  • 675
  • 8
  • 16
  • 1
    Robert, how do I launch that `.jnlp` file? When I start it with `javaws slave-agent.jnlp`, the program says: *The server rejected the connection: None of the protocols were accepted*. – Anton Shepelev Feb 17 '20 at 10:42
  • @Ant_222, I haven't seen this error firsthand, wish I had a more solid answer for you. It seems you're not alone in encountering this problem, there's even a Jenkins bug #. If you find an answer, please post it back here or the other StackOverflow thread (ref. below) as it may help someone else. In the meantime, perhaps these references can help: - JENKINS-29616: https://issues.jenkins-ci.org/browse/JENKINS-29616 - StackOverflow https://stackoverflow.com/questions/35309316/the-server-rejected-the-connection-none-of-the-protocols-were-accepted Best Regards, Robert – Robert Feb 19 '20 at 16:20
  • From JENKINS-29616: Example to try: java -jar agent.jar -jnlpUrl http://jenkinsurl:port/computer/windows-agent/slave-agent.jnlp -secret xxxxxxxx -workDir "C:\JenkinsSlave" – Robert Feb 19 '20 at 16:27
  • 1
    Problem solved—I had another connection to *Jenkins* active. I beg your pardon for not telling it as soon as I found it out. – Anton Shepelev Feb 20 '20 at 17:15
  • @Ant_222, You figured it out, then left a great artifact for anyone who has encounters the same. Excellent, thank you, and great work! – Robert Jul 09 '20 at 00:39
  • I cannot find the menu "file/install as a server" from the JNLP GUI. I see an small window with "connected" message on it. The only think I can do with this window is to close it. Please help me with this. I also tried the other command line, it was successful, but I don't see any option to install as a service. – tarekahf Apr 06 '22 at 20:06
  • JNLP (webstart) files are dead in a world were Java is version 11 or 17.... What people need to know is that all the JNLP gui did was auto setup WinSW (find it in github). Once we understood how to set the agent.jar to run as a windows service via WinSW we were all set. Its explained here: https://docs.cloudbees.com/docs/cloudbees-ci-kb/latest/client-and-managed-masters/how-to-install-windows-agents-as-a-service – Eric Weintraub Mar 10 '23 at 16:28
2

I have taken the

  • jenkins.exe
  • jenkins.exe.config
  • jenkins.xml

from a Jenkins 'master' installation. Next I have adapted the XML to contains the startup parameters for my slave. This gives something like:

<executable>%JAVA_HOME%\bin\java.exe</executable>
  <arguments>-Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -jar "C:\Tools\jenkins_slave\slave_95\agent.jar" -jnlpUrl http://jenkins_master:9090/computer/slave_95/slave-agent.jnlp -secret 999999999999999999999999999999999999999999 -workDir "C:\jenkins_slaves_workdir"</arguments>

In the XML I have also: - edited the fields for id,name,description to my preferences - removed the content of the 'extensions' block

After that I can just start the windows service running an admin shell on the slave using the command:

sc create <service_name> binpath= "C:\Tools\jenkins_slave\slave_95\Jenkins.exe" start= auto

Which is in principle the same as starting the Jenkins master service.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Monger39
  • 109
  • 8
  • 1
    This method is really the most flexible. `jenkins.exe install` within an Administrator-elevated `cmd.exe` console is also easier to use than `sc create [...]` – starlocke Feb 15 '21 at 18:43
1

The easiest way to set up Jenkins node (slave) as a service is to use https://nssm.cc/

  1. Unzip the nssm into C:\ drive
  2. Run command -> C:\nssm-2.24\win54>nssm install "JenkinsAgent"
  3. In the pop-up, you can give a path to startagent.bat available in Jenkins

You can also configure user you want to use for running jenkins jobs.

Screenshot

Joel Bodenmann
  • 2,152
  • 2
  • 17
  • 44
1

While other answers are effective, if you are like me and wanted the EXACT same agent experience you had before JavaWebstart died (when Oracle became the evil empire) and you couldn't just launch the JNLP file anymore... here it is....

The Java Agent (slave) gui that would allow you to configure the agent as a Windows Service did so via a open source package called WinSW (https://github.com/winsw/winsw).

All you have to do is utilize this tool in conjunction with the launch directives the Jenkins console provides you in the node configuration screens. While oddly NOT in the user guide for Jenkins, Cloudbees did make a KB about it here: https://docs.cloudbees.com/docs/cloudbees-ci-kb/latest/client-and-managed-masters/how-to-install-windows-agents-as-a-service

The net-net is this:

  • Ensure OpenJDK 17 is installed and in the system path so "java" works in any directory
  • Create a Agent Folder
  • Download a copy of the Agent.jar into the folder from your Jenkins Server (see the url for it in the node configuration screen)
  • Grab yourself a copy of WinSW-x64.exe and place it in the folder from here: https://github.com/winsw/winsw
  • Create yourself a WinSW-x64.xml from the template file also in the WinSW repo and fill in the various info like Service Name, etc...
  • Run WinSW-x64.exe INSTALL (of note: you can run UNINSTALL too)

If you look at a Windows Service install from the old java gui you will see they make a jenkins-slave.exe, thats WinSW-x64.exe, in fact you can simply rename WinSW-x64.exe but if you do the .xml file must match for the INSTALL command to work.

Cheers!

Eric Weintraub
  • 967
  • 1
  • 10
  • 23
0

service may need to be started explicitly after system startup. You may need to:

  1. go to the "log on" tab and start the service as a user with administrative rights on the machine

or

  1. Start the jenkins agent via windows scheduler via instructions here: https://wiki.jenkins-ci.org/display/JENKINS/Launch+Java+Web+Start+slave+agent+via+Windows+Scheduler
mac
  • 1