-
Notifications
You must be signed in to change notification settings - Fork 2
Systemd
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 Raspberry Pi boots up and handling any errors appropriately.
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.
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.
Conflicts=
A list of unit files that cannot be run at the same time as the current one. Suppose a.service
contains the line Conflicts=b.service
. Then if a.service
is running, then b.service
must be inactive (it cannot be started), or if a.service
is brought up, then b.service
is shut down. Can be used to ensure that b.service
doesn't run until a.service
dies, when used in conjunction with After=
.
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).
Type=
The type of the service. The "oneshot" type is used for services that spawn and then very quickly exit, so they are reported as having succeeded after they run, even if they aren't currently running. This is used for shm_start.service
and shm_stop.service
. The type "simple" is used for common long-lived services that are not supposed to die under normal circumstances; this is used for the three main Runtime services.
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.
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.
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
- Important
- Advanced/Specific