2017-12-20 14:37:20 +00:00
[#creating-new-systemd-services]
2018-02-26 16:43:59 +00:00
= Creating new systemd services
2017-12-20 14:37:20 +00:00
This example shows how to create a unit file for a custom service. Custom unit files are located in `/etc/systemd/system/` and have a `.service` extension. For example, a custom `foo` service uses `/etc/systemd/system/foo.service` unit file.
2020-07-26 18:35:01 +00:00
[discrete]
2020-08-05 13:14:35 +00:00
== Prerequisites
2017-12-20 14:37:20 +00:00
* You are logged in as a user with administrator-level permissions.
2020-07-26 18:35:01 +00:00
[discrete]
2020-08-05 13:14:35 +00:00
== Procedure
2017-12-20 14:37:20 +00:00
This procedure creates a basic configuration file to control the `foo` service.
. Create and edit the new configuration file:
+
----
2020-08-05 15:59:16 +00:00
# nano /etc/systemd/system/foo.service
2017-12-20 14:37:20 +00:00
----
. The next few steps describe each section its parameters to add to the file:
.. The `[Unit]` section provides basic information about the service. The `foo` service uses the following parameters:
+
`Description`::
2020-08-05 16:48:54 +00:00
A string describing the unit. _Systemd_ displays this description next to the unit name in the user interface.
2021-09-24 09:06:52 +00:00
`After`::
Defines a relationship with a second unit. If you activate the unit, _systemd_ activates it only after the second one. For example, the `foo` service might require network connectivity, which means the `foo` services specifies `network.target` as an `After=` condition.
2017-12-20 14:37:20 +00:00
+
The resulting `[Unit]` section looks like this:
+
----
[Unit]
Description=My custom service
2021-09-24 09:06:52 +00:00
After=network.target
2017-12-20 14:37:20 +00:00
----
.. The `[Service]` section provides instructions on how to control the service. The `foo` service uses the following parameters:
+
`Type`::
2020-08-05 15:59:16 +00:00
Defines the type of _systemd_ service. In this example, the `foo` service is a `simple` service, which starts the service without any special consideration.
2017-12-20 14:37:20 +00:00
`ExecStart`::
The command to run to start the service. This includes the full path to the command and arguments to modify the service.
+
The resulting `[Service]` section looks like this:
+
----
[Service]
Type=simple
ExecStart=/usr/bin/sleep infinity
----
2020-08-05 15:59:16 +00:00
.. The `[Install]` section provides instructions on how _systemd_ installs the service. The `foo` service uses the following parameters:
2017-12-20 14:37:20 +00:00
+
`WantedBy`::
2020-08-05 15:59:16 +00:00
Defines which service triggers the custom service if enabled with `systemctl enable`. This is mostly used for starting the custom service on boot. In this example, `foo.service` uses `multi-user.target`, which starts `foo.service` when _systemd_ loads `multi-user.target` on boot.
2017-12-20 14:37:20 +00:00
. The full `foo.service` file contains the following contents:
+
----
[Unit]
Description=My custom service
2021-09-24 09:06:52 +00:00
After=network.target
2017-12-20 14:37:20 +00:00
[Service]
Type=simple
ExecStart=/usr/bin/sleep infinity
[Install]
WantedBy=multi-user.target
----
+
Save the file.
2020-08-05 15:59:16 +00:00
. To make _systemd_ aware of the new service, reload its service files
+
----
# systemctl daemon-reload
----
2017-12-20 14:37:20 +00:00
. Start the custom `foo` service:
+
----
2018-01-21 17:04:05 +00:00
# systemctl start foo
2017-12-20 14:37:20 +00:00
----
. Check the status of the service to ensure the service is running:
+
----
$ systemctl status foo
● foo.service - My custom service
Loaded: loaded (/etc/systemd/system/foo.service; static; vendor preset: disabled)
Active: active (running) since Thu 2017-12-14 14:09:12 AEST; 6s ago
Main PID: 31837 (sleep)
Tasks: 1 (limit: 4915)
CGroup: /system.slice/foo.service
└─31837 /usr/bin/sleep infinity
Dec 14 14:09:12 dansmachine systemd[1]: Started My custom service.
----
2020-07-26 18:35:01 +00:00
[discrete]
2020-08-05 13:14:35 +00:00
== Related Information
2017-12-20 14:37:20 +00:00
2018-01-21 17:04:05 +00:00
* See link:#common-service-parameters[Common service parameters] for more information about the parameters used in this procedure.