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
JAVA_HOME=/usr/java/jdk-17.0.3.1 ## put your own JAVA_HOME

pid=`ps -ef | grep jenkins.war | grep -v 'grep'| awk '{print $2}'| wc -l`
if [ "$1" = "start" ];then
if [ $pid -gt 0 ];then
echo 'jenkins is running...'
else
## put your own path of java and jenkins.war
## What is >/dev/null 2>&1 ?
## https://www.linuxshelltips.com/redirect-output-in-linux/
nohup $JAVA_HOME/bin/java -jar /usr/local/src/jenkins/jenkins.war --httpPort=2022 >/dev/null 2>&1 &
fi
elif [ "$1" = "stop" ];then
exec ps -ef | grep jenkins | grep -v grep | awk '{print $2}'| xargs kill -9
echo 'jenkins is stop..'
else
echo "Please input like this:"./jenkins.sh start" or "./jenkins.sh stop""
fi

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
vi jenkins.service

Paste the following configuration into the jenkins.service file:

[Unit]
Description=Jenkins
After=network.target

[Service]
Type=forking
ExecStart=/jenkins.sh start
ExecReload=
ExecStop=/jenkins.sh stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

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:
status.png


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:

autostart.png


Reference