systemd

systemd, init - systemd system and service manager

systemd is a system and service manager for Linux operating systems. When run as first process on boot (as PID 1), it acts as init system that brings up and maintains userspace services. Separate instances are started for logged-in users to start their services.

systemd is usually not invoked directly by the user, but is installed as the /sbin/init symlink and started during early boot. The user manager instances are started automatically through the user@.service(5) service.

systemd is a collection of programs and libraries

  • init

  • systemctl

  • journalctl

  • process management

  • networkd (network management)

  • logind (login management)

  • journald (logs)

init

PID 1: it initializes the system

  • has power over other processes

    • if a process is orphaned because it parent process is terminated, the process will be re-parented by init

    • kills zombie processes

  • systemd is init but also handles system state and services

unit

A unit is any entity managed by systemd.

  • Service

  • Socket

  • Device

  • Mountpoint or Automount point

  • Swap File

  • Partition

  • startup target

  • watched filesystem path

  • group of externally created processes

unit file location Description
/lib/systemd/system/ standard systemd unit files
/usr/lib/systemd/system/ locally install packages (dnf)
/run/systemd/system/ transient
/etc/systemd/system/ custom unit files

vim /etc/systemd/system/tomcat.service

[Unit]
Description=Tomcat 9 servlet container
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/jre"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"
Environment="CATALINA_BASE=/opt/tomcat/latest"
Environment="CATALINA_HOME=/opt/tomcat/latest"
Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
ExecStart=/opt/tomcat/latest/bin/startup.sh
ExecStop=/opt/tomcat/latest/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
  • After=network.target: this unit should only be brought up after we have networking

  • Type=forking: the main process is going to start and bind to a port, forks, and creates child processes

  • ExecStart: command for starting the service

  • ExecStop: command for stopping the service

  • WantedBy=multi-user.target: who wants this process to run

systemctl

May be used to introspect and control the state of the systemd system and service manager.

systemctl enable

systemctl disable

systemctl status

sudo systemctl start 

sudo systemctl stop

sudo systemctl restart

systemctl daemon-reload

systemctl list-units --type=service

systemctl list-unit-files

journalctl

Query the systemd journal.

sudo journalctl -u tomcat2.service

slash “/” goes into search

-f : follow the log entries

–since “2020-12-28 10:24:00”

daemons

A daemon is a background, non-interactive program. It is detached from the keyboard and display of any interactive user. The word daemon for denoting a background program is from the Unix culture; it is not universal.

A service is a program which responds to requests from other programs over some inter-process communication mechanism (usually over a network). A service is what a server provides. For example, the NFS port mapping service is provided as a separate portmap service, which is implemented as the portmapd daemon.

A service doesn’t have to be a daemon, but usually is. A user application with a GUI could have a service built into it: for instance, a file-sharing application.

targets

manage relationships between units

  • figures out dependencies for you

  • multi-user.target: last target it hits after boot

  • network-pre.target: before the network comes up


References

Linux systemd - tutoriaLinux

Daemon vs Service