How to Create Service in Systemd?
Running Jenkins as a Service
If in the previous section you opted not to install Jenkins via the repo, and instead used the
WAR file, you won't yet be able to use Jenkins like a standard service.
Then, go to your /etc/systemd/system/ directory, and create a new file called [Link].
The following demonstration uses the nano editor, but naturally you can use whatever editing
tool you wish.
$ cd /etc/systemd/system/
$ sudo vim [Link]
Now, add the following lines to the new [Link] file. In a moment, we’ll go over exactly
what these lines accomplish.
/usr/local/bin/[Link]
[Unit]
Description=Jenkins Service
After=[Link]
[Service]
Type=simple
User=root
ExecStart=/usr/bin/java -jar /usr/local/bin/[Link]
Restart=on-abort
[Install]
WantedBy=[Link]
If you've seen configuration files before (INI files or similar), you'll recognize the structure being
used here. The bracketed text denotes a section heading. This means that, for instance,
[Service] declares a section called "Service," and all the assignments below it contain relevant
information that the system will then know how to find and relate to the section header.
A configuration file, this one included, is typically a text file – meaning it has no intrinsic meaning
to the computer. Rather, the text file will be parsed by some process, and that process will use
the headings and other information to find its way around. For this reason, it's technically
irrelevant how a given configuration file is laid out – as long as the program which reads it can
understand what everything means.
The first section, Unit, contains only two configuration directives. The first is simply a name. It
can be whatever name you'd like, but ideally it should be one that uniquely identifies the new
process. The second directive states what service, if any, is necessary for the current service to
start.
In the next section, the Type directive allows you to select what type of startup this service will
use. The value simple indicates that the process noted in the later directive ExecStart will be the
primary process of the service being created. Really, type is unnecessary, as simple is assumed
when type is unspecified, but we are leaving it in for clarity.
User specifies which user has control over this process, and Restart is used to indicate that, in
this case, if the process terminates but the exit code implies error, the service will be restarted.
This is useful in maintaining the continuity of the service in case of unexpected crashes.
As mentioned, ExecStart is the directive where we indicate what process is to become the main
action of the service. This directive represents the main wrapper for Jenkins – the service will
run the WAR through Java rather than treating it a foreground process.
Finally, in the Install section, [Link] indicates a target, called a runlevel prior to
CentOS 7. It provides for the system a sense of what resources to provide this service and what
amount of intensity will be required by the user.