systemd

Introduction

Systemd primary task is to manage the boot process and provides information about it. It replaces SysVinit and makes a server boot quicker because it uses fewer scripts and tries to run more tasks in parallel. In addition, Systemd handles the system event log through Journald. The global Systemd configuration is stored in the /etc/systemd directory. To get a better understanding about the problems Systemd solves, read this thread from an ArchLinux maintainer.

Unit

The basic object that systemd manages and acts upon is a “unit”. systemd has 12 unit types:

  • service: system services
  • Target: group of units
  • Automount: filesystem auto-mountpoint
  • Device: kernel device names, which you can see in sysfs and udev
  • Mount: filesystem mountpoint
  • Path: file or directory
  • Scope: external processes not started by systemd
  • Slice: a management unit of processes
  • Snapshot: systemd saved state
  • Socket: IPC (inter-process communication) socket
  • Swap: swap file
  • Timer: systemd timer.

The most common unit type is a “service” (indicated by a unit file ending in .service). A unit file is a plain text ini-style file that encodes information about a service, a socket, a device, a mount point, an automount point, a swap file or partition, a start-up target, a watched file system path, a timer controlled and supervised by systemd. The common configuration options of all the unit types need to be configured in the [Unit] or [Install] sections of the unit files. In addition, each unit may have a type-specific section, e.g. [Service] for a service unit.

[Unit] Section Options

The [Unit] section carries generic information about the unit that is not dependent on the type of unit, including:

  • Description= A free-form string describing the unit.
  • Requires= Configures requirement dependencies on other units.
  • Wants= A weaker version of Requires=.
  • Before=, After= A space-separated list of unit names to configure ordering dependencies between units.

[Install] Section Options

The [Install] section carries installation information for the unit, including:

  • WantedBy=, RequiredBy= This option may be used more than once, or a space-separated list of unit names may be given. The primary result is that the current unit will be started when the listed unit is started.
  • Also= Additional units to install/deinstall when this unit is installed/deinstalled.

The [Install] section is used by the enable and disable commands of the systemctl tool during installation of a unit.

For a complete list of both the [Unit] and the [Install] section, see the systemd.unit manual page.

Shared execution environment options

Unit configuration files for services, sockets, mount points, and swap devices share a subset of configuration options which define the execution environment of spawned processes, including:

  • WorkingDirectory= Sets the working directory for executed processes.
  • Environment= Sets environment variables for executed processes.
  • StandardInput= Controls where file descriptor 0 (STDIN) of the executed processes is connected to.
  • StandardOutput= Controls where file descriptor 1 (STDOUT) of the executed processes is connected to.

For a complete list of execution environment options, see the systemd.exec manual page.

Service unit

A unit configuration file whose name ends in “.service” encodes information about a process controlled and supervised by systemd. The common configuration items are configured in the generic “[Unit]” and “[Install]” sections. The service specific configuration options are configured in the “[Service]” section and include:

  • Type= Configures the process start-up type for this service unit. One of simple, forking, oneshot, dbus, notify or idle.
  • ExecStart= Commands with their arguments that are executed when this service is started.
  • Restart= Configures whether the service shall be restarted when the service process exits, is killed, or a timeout is reached.

For a complete list, see the systemd.service manual page.

Timer unit

A unit configuration file whose name ends in “.timer” encodes information about a timer controlled and supervised by systemd, for timer-based activation.
For each timer file, a matching unit file must exist, describing the unit to activate when the timer elapses.
The common configuration items are configured in the generic “[Unit]” and “[Install]” sections.
The timer specific configuration options are configured in the [Timer] section and include:
OnActiveSec=, OnBootSec=, OnStartupSec=, OnUnitActiveSec=, OnUnitInactiveSec=
Defines monotonic timers relative to different starting points.
Multiple directives may be combined of the same and of different types.
OnCalendar=
Defines realtime (i.e. wallclock) timers with calendar event expressions.

For a complete list, see the systemd.timer manual page.
For an example, see: http://www.linux-magazine.com/Issues/2017/200/Tutorials-Systemd

Socket unit

A unit configuration file whose name ends in “.socket” encodes information about an IPC or network socket or a file system FIFO controlled and supervised by systemd, for socket-based activation.
For each socket file, a matching service file must exist, describing the service to start on incoming traffic on the socket.
Depending on the setting of the Accept= option described below, this .service unit must either be named like the .socket unit (Accept=true, foo.socket needs foo.service) or it must be a template unit named the same way (Accept=true, foo.socket needs foo@.service).
The common configuration items are configured in the generic [Unit] and [Install] sections.
The socket specific configuration options are configured in the [Socket] section and include:
ListenStream=, ListenDatagram=, ListenSequentialPacket=
Specifies an address to listen on for a stream (SOCK_STREAM), datagram (SOCK_DGRAM), or sequential packet (SOCK_SEQPACKET) socket, respectively. The address can be written in various formats:
Accept=
If true, a service instance is spawned for each incoming connection. If false, all listening sockets themselves are passed to the started service unit. For performance reasons, it is recommended to write new daemons only in a way that is suitable for Accept=false. Setting Accept=true is mostly useful to allow daemons designed for usage with inetd to work unmodified with systemd socket activation.

For a complete list, see the systemd.socket manual page: https://www.freedesktop.org/software/systemd/man/systemd.socket.html
For an example, see: systemd for Developers I http://0pointer.de/blog/projects/socket-activation.html and https://www.enricozini.org/blog/2017/debian/systemd-09-sockets/

Target unit

A unit configuration file whose name ends in “.target” encodes information about a target unit of systemd, which is used for grouping units and as well-known synchronization points during start-up.
The common configuration items are configured in the generic [Unit] and [Install] sections.
A separate [Target] section does not exist.
For more information, see the systemd.target manual page: https://www.freedesktop.org/software/systemd/man/systemd.target.html

systemctl

The main tool to manage services on a systemd enabled server is the systemctl command.
Print a short help text and exit:
# systemctl -h
Print a short version string and exit:
# systemctl –version
All of the normal init system commands have equivalent actions with the systemctl command:
# systemctl start|stop|enable|disable|status foo.service
Enabling a unit hooks it up to a certain boot “target”, causing a service to start automatically at boot.
Show all installed unit files (of type service):
# systemctl list-unit-files [–type=service]
Get all of the unit files that systemd has listed as “active”:
# systemctl [list-units]

journalctl

A systemd component called journald collects and manages journal entries from all parts of the system.
This is basically log information from applications and the kernel.
The tool to query the systemd journal is the journalctl command.
Show all log entries, starting at the oldest entry:
# journalctl
Show only kernel messages:
# journalctl -b
Show all of the journal entries for a unit, optionally limited to the current boot:
# journalctl [-b] -u foo.service

For more information, see the journalctl manual page: https://www.freedesktop.org/software/systemd/man/journalctl.html

References

  • https://www.freedesktop.org/wiki/Software/systemd/
  • http://0pointer.de/blog/projects/systemd.html
  • https://www.certdepot.net/rhel7-get-started-systemd/
  • Systemd Essentials https://www.digitalocean.com/community/tutorials/systemd-essentials-working-with-services-units-and-the-journal
  • https://www.linux.com/learn/understanding-and-using-systemd
  • http://www.linux-magazine.com/Issues/2017/200/Tutorials-Systemd
  • systemd.unit — Unit configuration
  • How To Use Journalctl