Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install Jenkins Ubuntu slave as a service?

Tags:

linux

jenkins

I have a Ubuntu 16.04 LTS machine where I am successfully connected to the Jenkins server via JNLP connection. Steps I took for the connection are the following:

  1. Create a directory in the slave called /home/MyUbuntu/Jenkins
  2. Download both agent.jar and slave-agent.jnlp files into the directory
  3. Run this command from the terminal:

java -jar agent.jar -jnlpUrl http://my-jenkins-server:8080/computer/MyNode/slave-agent.jnlp -secret 6f8bb3250d6dbcda77979797997b0ea6bcaaa064785d558c0e4ea07d03 -workDir "/home/MyUbuntu/Jenkins"

The connection is successful.

Problem: Once I close the terminal the connection gets disconnected.

Question: How do I add this as a service in Ubuntu 16.04 LTS so whenever the machine is rebooted it starts as a startup???

like image 583
Shahboz Avatar asked Jul 20 '18 15:07

Shahboz


1 Answers

If you use System D, add a file like this to /etc/systemd/system/.

[Unit]
Description=Jenkins slave connection
Wants=network.target
After=network.target

[Service]
ExecStart=java -jar agent.jar -jnlpUrl http://jenkinsurl:port/endpoint.jnlp -secret 4lph4num3r1cs3cr3t -workDir "/base/path/of/your/jenkinsjar"
Restart=always
WorkingDirectory=/base/path/of/your/jenkinsjar
User=my-user
Group=my-group
RestartSec=20s


[Install]
WantedBy=multi-user.target
Alias=jenkins.service

Permissions and ownership of the file may vary based on the service or OS. Long list files in /lib/systemd/system/ to get an idea of what perms you need or want (probably root:root 644).

Notice the command is the command that Jenkins provides for you when you create an agent jar. Just use that for ExecStart.

For user and group, I use the user that owns the directory where the Jenkins workspace is located. For example, if the Jenkins workspace is in /home/ubuntu, I specify ubuntu as user and group.

After that...

Prefix these with sudo if you're not running as root:

Probably a good idea to reload System D: systemctl daemon-reload.

Start: systemctl start jenkins.service. Notice this command pertains to the last line of the file Alias.

Enable it if you want the service to start with your computer: systemctl enable jenkins.service.

Here are the Git Gists I based my file on:

https://gist.github.com/unakatsuo/d4711f52a0ab0b9bc8010018149a7e84 https://gist.github.com/dragolabs/05dfe1c0899221ce51204dbfe7feecbb

I'm sure there's a lot more that can be done for the service config but in my case, I manage a lot of different servers and just need the thing to start automatically after boot!

like image 137
JoeS Avatar answered Dec 22 '22 00:12

JoeS