Systemd Units

Systemd is a system and service manager for Linux. It is the default init system for a lot of distros and is awesome to manage services, sockets, timers, (and much more but we dont talk about that).

What is a unit?

A unit is a configuration file that describes a process or a group of processes. It configures how the process should be started, stopped, restarted, and so on. It declares the dependencies between units and the order in which they should be started. It can also configure the resources that the process requires, such as the amount of memory or the number of open files.

A unit can be a service when ending in .service, this defines a start, stop and restart command. A service can be managed manually with systemctl {start|stop|restart} <service>, but it is also possible to enable the service to start on boot with systemctl enable <service>.

A unit can also be a socket when ending in .socket, this defines a socket that can be used to start a service.

Finally unit can also be a timer when ending in .timer, this defines a timer that can start a service at a specific time (yep its a cron-job).

How to create your own unit?

A unit is a text file that contains a set of key-value pairs similar to the ini file format, the format is defined in man (5) systemd.unit. The file is divided into sections, each section is defined by a header in square brackets. For example the nginx service is defined as:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

The first section [Unit] defines the service and its dependencies. The second section [Service] defines how the service should be started, stopped and restarted. The last section [Install] defines how the service should be installed.

This file can be placed in /etc/systemd/system/ for a system-wide service or ~/.config/systemd/user/ for a user service, which is then managed by passing the user flag to systemctl systemctl start --user nginx.

Why are they so good?

Systemd units are awesome because they are flexible and can be used to manage a lot of different things. They have a lot of features that make them powerful. For example you can make the service restart if it crashes, by adding Restart=on-failure to the [Service] section. This is similar to the --restart=on-failure flag in docker. You can also make the service restart after a specific time by adding RestartSec=100ms to the [Service] section.

You can even restart the entire operating system if a service crashes by adding FailureAction=reboot to the [Service] section. This is especially useful when you are running on embedded devices and want to make sure that the device is running properly.

Note that there are some pitfalls when using FailureAction=reboot and you should limit the times the service is restarted to prevent a reboot loop. By default the service is restarted 5 times in 10 seconds, after that the service is marked as failed and the system is rebooted.

This can be configured with the StartLimitIntervalSec and StartLimitBurst

You could also make the service send an email, http request or even a message to a slack channel if it crashes by adding OnFailure=error-report.service to the [Service] section. This will execute the error-report service (you can set the error-report service type to oneshot) if the service crashes or fails to start.

The services can also be configured to start in isolation or with some mounted folder or interface. They can be configured for many different facets of the system and can be setted up to make sure the system is always running properly.

Additionally, all the services can be managed with a single command and will all log to the journal. This permits you to manage the services and to debug issues with a single command.

To validate the configuration of a service you can use systemctl-analyze verify <service> which will check if the service is valid and if the dependencies are met.

Conclusion

In conclusion, systemd is an excellent system and service manager for Linux that offers a wide range of features for developers to manage services efficiently. Its units provide a flexible way to manage processes and their dependencies, allowing developers to focus on building and deploying their applications without worrying about system management. Additionally, systemds powerful features, such as service isolation, resource allocation, and automatic restart on failure, provide developers with greater control over their applications behavior and ensure that the system is always running optimally. Furthermore, with systemds unified interface and logging to the journal, developers can manage and debug their services. In summary, systemd is a great tool for developers who want to manage their services more efficiently and with greater control, and it is definitely the tool to reach for when choosing a system and service manager for Linux.