Configuring Jenkins as a Systemd Service in Linux
In this tutorial, we will learn how to set up Jenkins as a Systemd service on Linux. The benefit of using a Systemd service is that it provides a simple way to start, stop, and monitor your Jenkins service. We will create a shell script and a Systemd service file, and then enable Jenkins to start automatically at boot time.
Step1: create jenkins.sh
There are two ways to create the Jenkins shell script:
Option 1: Code in a Vim editor
You have two options to create the Jenkins.sh file:
vi jenkins.sh |
Then, add the following code:
##!/bin/bash |
Option 2: Code in IDE and Upload it
You can also code the Jenkins.sh file in an IDE and upload it to your server. If you are using Windows, be aware of the line ending issue between Windows and Linux. You can use the following command to handle it:
sed -i "s/\r//" jenkins.sh
Step2: Set Permissions for the jenkins.sh Script
To set the executable permission for the jenkins.sh script, use the following command:
chmod +x jenkins.sh |
Step-Check: Check the start/stop via jenkins.sh
Start
/jenkins.sh start |
Stop
/jenkins.sh stop |
Step3: Create the jenkins.service File
create a jenkins.service
file using the following command:
cd /lib/systemd/system |
Paste the following configuration into the jenkins.service
file:
[Unit] |
Step-Check: Check jenkins.service
daemon-reload
Now, reload systemd to make it aware of the new service file and start the Jenkins service using the following commands:
systemctl daemon-reload |
start service
systemctl start jenkins.service |
service status
You can check the status of the Jenkins service using the following command:
systemctl status jenkins.service |
If everything is working correctly, you should see a message similar to the following:
Step4: Set jenkins.service to Boot Up
To make Jenkins start automatically when the system boots, use the following command:
systemctl enable jenkins.service |
Step-Check: Check All Service
Finally, you can check if the Jenkins service exists by running the following command:
systemctl list-units --type=service |
If you need to stop the Jenkins service, you can use the following command:
systemctl stop jenkins.service |
The Ultimate Test
To confirm that Jenkins autostarts, you can restart the server and run the following command:
ps -ef | grep -i jenkins |
If Jenkins has started successfully, you should see output similar to the following:
Reference
- To learn more about shell scripts, refer to this: https://nataliexselina.github.io/shell-script/
- For more information on service, refer to this: https://linuxconfig.org/how-to-create-systemd-service-unit-in-linux