Skip to content

Systemd

kaqqy edited this page Aug 15, 2020 · 11 revisions

Introduction

Systemd is a set of tools on Linux used to configure the behavior of processes with regards to when they startup, what happens before/after they startup, what to do when they error, etc. This provides a robust backend for all processes in Runtime by allowing those processes to start in a specific order after the device boots up and handling any errors appropriately.

Unit Files

Each process is configured by its corresponding unit file. Unit files should be named with the .service extension and be placed in the /etc/systemd/system directory. Each unit file is split by sections, which start with [SectionName] on one line and have settings on the following lines.

Here is a description for some of the settings. Check the links in Additional Resources for more settings.

[Unit]

Description=The description for the process being managed.

BindsTo=A list of unit files for other processes that this process should be bound to. For example, if it's set to "a.service b.service c.service", then when one of the processes managed by a/b/c.service starts/restarts/stops, then the process managed by this unit file will also start/restart/stop. This setting is one-directional, so if the process managed by this unit file starts/restarts/stops, then it won't affect the processes managed by a/b/c.service

After=A list of unit files that must be running before this process starts (i.e. this process must run after the processes in the listed unit files run). When combined with "Type=oneshot" in the "Service" section, this process can only start after the processes in the unit files complete.

OnFailure=A list of unit files that run when this process fails. Failure is defined as an unclean exit code (non-zero exit code) or an unclean signal (any signal that isn't SIGHUP, SIGINT, SIGTERM, or SIGPIPE).

[Service]

Type=The type of the service. This is set to type "oneshot" in order to change the behavior of "After" in the "Unit" section to only allow this process to start after all processes in the listed unit files finish.

User=The user running the process. This matters for file permissions (e.g. only a specific user has permissions to read/write to a file).

WorkingDirectory=The directory in which the process is run from. This matters for relative file paths used in some processes.

ExecStart=Command for starting the process. Only use absolute paths, no relative paths. For executables (which includes the output of compiling c programs), the command is just the absolute path to the executable. If you have something like a python script that isn't executable, the command would be something like "/path/to/python /location/of/python/script.py"

Restart=Condition for restarting the process. Setting the option to "on-failure" means restarting on an unclean exit code (non-zero exit code) or an unclean signal (any signal that isn't SIGHUP, SIGINT, SIGTERM, or SIGPIPE).

KillSignal=The signal sent by systemd in order to forcibly terminate processes.

[Install]

WantedBy=A list of unit files that "want" this process to start. Basically just set it to "multi-user.target" since it's unlikely this setting will be set to anything else. Effectively, it will cause this process to start on startup.

Useful Commands

Here is a list of useful commands you can use when working with systemd.

Start a process manually once

sudo systemctl start filename.service

Stop a process

sudo systemctl stop filename.service

Enable a process (allows the process to start on bootup)

sudo systemctl enable filename.service

Disable a process (prevent the process from starting on bootup)

sudo systemctl disable filename.service

View the logs/status of a process

sudo systemctl status filename.service

View the logs of multiple processes together (can append arbitrarily many "-u filename")

journalctl -u filename1 -u filename2

Additional Resources