Custom systemd Unit and Unit File: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(30 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
=Internal=
* [[Systemd_Operations#Custom_systemd_Unit|systemd Operations]]
* [[Systemd_Concepts#Create_a_Custom_Unit_File_for_a_Service|systemd Concepts]]
* [[Minecraft]]
 
=Overview=
 
This article describes the procedure to configure an arbitrary service <code>minecraft</code> to be managed by <code>systemd</code>. It includes the creation of corresponding [[Systemd_Concepts#Unit_Files|unit file]] and <code>systemd</code> configuration to start and stop the service automatically at boot, respectively shutdown.


* https://access.redhat.com/solutions/912263
=Create the Unit File=
Create the <code>/etc/systemd/system/minecraft.service</code> unit file. As root:
<syntaxhighlight lang='bash'>
touch /etc/systemd/system/minecraft.service
chmod 644 /etc/systemd/system/minecraft.service
</syntaxhighlight>
For more details on the location of the unit files, see: {{Internal|Systemd_Concepts#Unit_File_Location|systemd Concepts &#124; Unit File Location}}


=Internal=
=Configure the Unit File=
 
==Process Started and Stopped by Auxiliary Scripts==
This is common for Java programs, <code>/home/minecraft/minecraft-server/minecraft</code> is a wrapper script that starts the JVM and puts in background like this:
<syntaxhighlight lang='bash'>
java -server -jar ... > $(dirname $0)/logs/stdout-and-sterr.log 2>&1 &
</syntaxhighlight>
The unit file configuration is similar to:
<syntaxhighlight lang='bash'>
[Unit]
Description=Minecraft service
After=network.target
 
[Service]
Type=oneshot
ExecStart=/home/minecraft/minecraft-server/minecraft start
ExecStop=/home/minecraft/minecraft-server/minecraft stop
RemainAfterExit=yes
 
[Install]
WantedBy=multi-user.target
</syntaxhighlight>
Also see: {{Internal|Systemd_Concepts#Type.3Doneshot|systemd Concepts &#124; <tt>oneshot</tt>}}


* [[systemd#Configuration|systemd]]
==Daemon Process that Forks and Creates Its Own PID File==


=Overview=
<syntaxhighlight lang='bash'>
[Unit]
Description=MyService
After=network.target


This article describes the procedure to configure an arbitrary service <tt>myservice</tt> to be managed by <tt>systemd</tt>. It includes the creation of corresponding unit file and systemd configuration to start/stop the service automatically at boot/shutdown.
[Service]
ExecStart=/opt/myservice/bin/myservice
Type=forking
PIDFile=/var/run/myservice.pid


<font color=red>
[Install]
=Important Note=
WantedBy=multi-user.target
</syntaxhighlight>
Also see: {{Internal|Systemd_Concepts#Type.3Dforking|Systemd Concepts &#124; <tt>forking</tt>}}


This procedure was not tested step-by-step. When I have a concrete case that can be used for testing, revisit and update.
=Notify <tt>systemd</tt> of the Existence of the New Unit File=
</font>
<syntaxhighlight lang='bash'>
systemctl daemon-reload
</syntaxhighlight>
<font color=darkkhaki>Is this really necessary? Why exactly? At this point the file is completely unknown to the system, it as not enabled.</font>
For more details on <code>daemon-reload</code> see: {{Internal|Systemd_Concepts#Daemon_Reload|systemd Concepts &#124; Daemon Reload}}


=Create the Unit File=
=Enable at Boot=
<syntaxhighlight lang='bash'>
systemctl enable minecraft.service
</syntaxhighlight>
<syntaxhighlight lang='bash'>
Created symlink from /etc/systemd/system/multi-user.target.wants/minecraft.service to /etc/systemd/system/minecraft.service.
</syntaxhighlight>


Create the <tt>/etc/systemd/system/myservice.service</tt> unit file. For more details on the location of the unit files, see [[]].
=Exercise the Service=


<pre>
<syntaxhighlight lang='bash'>
touch /etc/systemd/system/myservice.service
systemctl start minecraft
chmod 644 /etc/systemd/system/myservice.service
systemctl status minecraft
</pre>
systemctl restart minecraft
systemctl stop minecraft
</syntaxhighlight>

Latest revision as of 03:03, 3 October 2023

Internal

Overview

This article describes the procedure to configure an arbitrary service minecraft to be managed by systemd. It includes the creation of corresponding unit file and systemd configuration to start and stop the service automatically at boot, respectively shutdown.

Create the Unit File

Create the /etc/systemd/system/minecraft.service unit file. As root:

touch /etc/systemd/system/minecraft.service
chmod 644 /etc/systemd/system/minecraft.service

For more details on the location of the unit files, see:

systemd Concepts | Unit File Location

Configure the Unit File

Process Started and Stopped by Auxiliary Scripts

This is common for Java programs, /home/minecraft/minecraft-server/minecraft is a wrapper script that starts the JVM and puts in background like this:

java -server -jar ... > $(dirname $0)/logs/stdout-and-sterr.log 2>&1 &

The unit file configuration is similar to:

[Unit]
Description=Minecraft service
After=network.target

[Service]
Type=oneshot
ExecStart=/home/minecraft/minecraft-server/minecraft start
ExecStop=/home/minecraft/minecraft-server/minecraft stop
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Also see:

systemd Concepts | oneshot

Daemon Process that Forks and Creates Its Own PID File

[Unit]
Description=MyService
After=network.target

[Service]
ExecStart=/opt/myservice/bin/myservice
Type=forking
PIDFile=/var/run/myservice.pid

[Install]
WantedBy=multi-user.target

Also see:

Systemd Concepts | forking

Notify systemd of the Existence of the New Unit File

systemctl daemon-reload

Is this really necessary? Why exactly? At this point the file is completely unknown to the system, it as not enabled.

For more details on daemon-reload see:

systemd Concepts | Daemon Reload

Enable at Boot

systemctl enable minecraft.service
Created symlink from /etc/systemd/system/multi-user.target.wants/minecraft.service to /etc/systemd/system/minecraft.service.

Exercise the Service

systemctl start minecraft
systemctl status minecraft
systemctl restart minecraft
systemctl stop minecraft