Skip to main content

How to create an OS boot service

Introduction

This document serves as support for the creation of services in OS, i.e., services that run in the operating system to perform specific tasks.

Where are the services?

The services to be run by the operating system must be located in the following directory: /etc/systemd/system. Within this directory, the file ('.service') in which the new service is created must be created, i.e. example.service

Structure of .service file

This files have 3 big groups : [Unit], [Service], [Install]

  • [Unit]

    • Description = A short description of the unit.
    • Documentation = A list of URIs referencing documentation.
    • Before, After = The order in which units are started.
    • Requires = If this unit gets activated, the units listed here will be activated as well. If one of the other units gets deactivated or fails, this unit will be deactivated.
    • Wants = Configures weaker dependencies than Requires. If any of the listed units does not start successfully, it has no impact on the unit activation. This is the recommended way to establish custom unit dependencies.
    • Conflicts = If a unit has a Conflicts setting on another unit, starting the former will stop the latter and vice versa.
  • [Service]

    • Type = Configures the process start-up type
    • ExecStart = Commands with arguments to execute when the service is started.
    • ExecStop = Commands to execute to stop the service started via ExecStart.
    • ExecReload = Commands to execute to trigger a configuration reload in the service.
    • Restart = With this option enabled, the service shall be restarted when the service process exits, is killed, or a timeout is reached with the exception of a normal stop by the systemctl stop command.
    • RemainAfterExit = If set to True, the service is considered active even when all its processes exited. Useful with Type = o neshot. Default value is False.
  • [Install]

    • Alias = A space-separated list of additional names for the unit. Most systemctl commands, excluding systemctl enable, can use aliases instead of the actual unit name.
    • RequiredBy, WantedBy = The current service will be started when the listed services are started. See the description of Wants and Requires in the [Unit] section for details.
    • Also = Specifies a list of units to be enabled or disabled along with this unit when a user runs systemctl enable or systemctl disable.

Example of a simple service

service

Important commands that can be useful

Reload the service files to include the new service. (Mandatory)
sudo systemctl daemon-reload

Start your service (Mandatory)
sudo systemctl start your-service.service

To check the status of your service (Optional)
sudo systemctl status example.service
sudo systemctl -l --no-pager status example.service (This command give you a more detailed information, super important for DEBUG)

To enable your service on every reboot (Optional)
sudo systemctl enable example.service