systemd

systemd is a suite of basic building blocks for a Linux system. It provides a system and service manager that runs as PID 1 and starts the rest of the system.

Units

Instead of using a set of complex Bash shell scripts, the systemd init system controls system and service operations with various types of unit files. Each unit file has a filename with a filename extension that describes which type of unit it is. (Tevault 2022, 18)

Unit files are written in three locations:

  • /etc/systemd/system: the default location for unit files that either come with the operating system.
  • /run/systemd/system
  • /usr/lib/systemd/system

Types of Unit Files

Here's a list of the more common types:

  systemctl -t help
  • service: These are the configuration files for services.
  • socket: Sockets can either enable communication between different system services or they can automatically wake up a sleeping service when it receives a connection request.
  • slice: Slice units are used when configuring cgroups.
  • mount and automount: These contain mount point information for filesystems that are controlled by systemd. Normally, they get created automatically, so you shouldn't have to do too much with them.
  • target: Target units are used during system startup, for grouping units and for providing well-known synchronization points.
  • timer: Timer units are for scheduling jobs that run on a schedule. Replaces the old cron system.
  • path: Path units are for services that can be started via path-based activation.
  • swap: Swap units contain information about your swap partitions.

Commands

  systemctl list-units
  # You can also view specific types of units with the -t option.
  systemctl list-units -t service
  # and also filter it by the service's state
  systemctl list-units -t service --state=dead
  systemctl list-dependencies

Service

Service units are the equivalent of init scripts on old SysV systems. We'll use them to configure our various services, which we used to call daemons in the old days. A service can be pretty much anything that you want to start automatically and run in the background. Examples of services include Secure Shell, your web server of choice, a mail server, and various services that are required for proper system operation. (Tevault 2022, 32)

Unit Section

The top section of a service file is the [Unit] section, which contains the following parameters:

  • Description
  • After: Which targets are dependencies for the service.
  • Documentation

To check more options not mentioned here:

man systemd.unit

Service Section

Some usefull service options include:

  • Type: Options such as forking, notify.
  • Environment
  • EnvironmentFile
  • ExecStartPre: Tells systemd what to run before the ExecStart phase.
  • ExecStart, ExecStop, ExecReload: Command lines for starting, stopping and reloading the service.
  • PrivateTmp: When set to true, this parameter forces the service to write its temporary files to a private /tmp/ directory that nobody else can access.
  • Restart: Options such as on-abort, on-failure or always.

Install Section

Controls what happens when you enable or disable a unit.

  • WantedBy

References:

Tevault, Donald A. 2022. Linux Service Management Made Easy with Systemd. 1st ed. Packt Publishing.

Backlinks: