Custom systemd Unit and Unit File: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 36: Line 36:
==Daemon Process that Forks and Creates Its Own PID File==
==Daemon Process that Forks and Creates Its Own PID File==


<pre>
<syntaxhighlight lang='bash'>
[Unit]
[Unit]
Description=MyService
Description=MyService
Line 48: Line 48:
[Install]
[Install]
WantedBy=multi-user.target
WantedBy=multi-user.target
</pre>
</syntaxhighlight>


For more details on the content of the unit files, see [[systemd Concepts#Unit_File_Structure|systemd Concepts - Unit File Structure]].
For more details on the content of the unit files, see [[systemd Concepts#Unit_File_Structure|systemd Concepts - Unit File Structure]].

Revision as of 23:05, 19 August 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

[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

For more details on the content of the unit files, see systemd Concepts - Unit File Structure.

Notify systemd of the existence of the new unit file

systemctl daemon-reload

Enable at Boot

systemctl enable myservice.service

Exercise the Service

systemctl start myservice
systemctl status hello
systemctl restart hello
systemctl stop hello