Skip to content

Systemd

Systemd is a software suite that provides an array of system components for Linux operating systems. Its main aim is to unify service configuration and behavior across Linux distributions.

Systemd's primary component is a "system and service manager" an init system used to bootstrap user space and manage user processes. It also provides replacements for various daemons and utilities, including device management, login management, network connection management and event logging.

How to manage systemd services and units

Official systemctl documentation.

The systemctl command is the central management tool for controlling the init system.

In the following table you can find some interesting commands related to systemctl.

Command Description
systemctl start <service_name> It is used to start a service during the current session.
systemctl stop <service_name> It is used to stop a service during the current session.
systemctl restart <service_name> It is used to restart a running service.
systemctl reload <service_name> It is used to reload the configuration files of a service.
systemctl enable <service_name> It is used to start services automatically at boot.
systemctl disable <service_name> It removes the symbolic link which makes the services start
automatically at boot.
systemctl status <service_name> It is used to see the status of a determined service.
systemctl list-units It shows a list of all of the active units that systemd knows about.

How to use journalctl to view and manipulate systemd logs

Official journalctl documentation.

The journald daemon is a centralized management solution for logging all kernel and userland processes.

To see the logs that the journald daemon has collected, use the journalctl command.

In the following table you can find some interesting commands related to journalctl.

Command Description
journalctl -u node-red --since "2021-11-10" --until "2021-11-11 03:00" The logs related with Node-RED service from 2021-11-10
to 2021-11-11 until 03:00 will be displayed.
journalctl -u node-red --since yesterday The logs related with Node-RED service from the previous
day until the current moment will be displayed.
journalctl -u node-red --since 09:00 --until "1 hour ago" The logs related with Node-RED service from 09:00 until
1 hour before the command was sent will be displayed.
journalctl _PID=177 You will see the logs of the process with PID = 177.
journalctl -n 20 You will see the last 20 entries.
journalctl --list-boots It is used to see the boots that journald knows about.
journalctl -b -1 It is used to see the journal from the previous boot.
journalctl --verify It checks the journal file for internal consistency.
journalctl --disk-usage It shows the sum of the disk usage of all archived
and active journal files.
journalctl --vacuum-size=20M It removes the oldest archived journal files until
the disk space they use falls below the specified size.
journalctl --vacuum-time=2weeks It removes all archived journal files contain
no data older than the specified timespan.

Timesyncd

Official timesyncd documentation.

Timesyncd is a system service that may be used to synchronize the local system clock with a remote Network Time Protocol (NTP) server. The eManager integrates this NTP service (systemd-timesyncd), to syncronize time to public NTP hosts.

The public NTP servers used are:

  • time1.google.com
  • time2.google.com
  • time3.google.com
  • time4.google.com

This service is started by default and the time synchronization will be carried out at the beggining.

In the following table you can find some interesting commands related to this service.

Command Description
timedatectl It shows the current settings.
timedatectl timesync-status It shows the current status of systemd-timesyncd service.
timedatectl set-timezone UTC It is used to set the coordinated universal time (UTC).
timedatectl set-timezone America/Santiago It is used to set the Santiago de Chile zone.
timedatectl list-timezones It shows the available timezones.
timedatectl set-time '2020-01-01 00:00' It is used to set new time.

Resolved

Official resolvectl documentation.

The resolvectl service may be used to resolve domain names, IPv4 and IPv6 addresses, DNS resource records and services.

In the following table you can find some interesting commands related to this service.

Command Description
resolvectl status It shows the global and per-link DNS settings currently in effect.
resolvectl status eth0 It shows eth0 interface DNS settings currently in effect.
resolvectl status wlan0 It shows wlan0 interface DNS settings currently in effect.
resolvectl status ppp0 It shows ppp0 interface DNS settings currently in effect.
resolvectl query www.pickdata.net It resolve domain names, as well as IPv4 and IPv6 addresses.
resolvectl flush-caches It cleans the DNS cache on Linux.

How to create a systemd.service

Official systemd.service documentation.

Two files are needed:

  • executable file: binary file, bash script, python script, etc.
  • service file: *.service INI-like file that configures the service. It defines when the executable file is run, restarted, etc.

Step 1. Create an executable file

Create the <executable_name> file that will be run by the service. It is a good practice to place it at /usr/local/bin.

Give it execute permission with

chmod +x /usr/local/bin/<executable_name>

Step 2. Create a configuration file for the service

Create a file in /lib/systemd/system

nano /lib/systemd/system/<service_name>.service

of the form

[Unit]
# unitOption1=value1
# unitOption2=value2

[Service]
ExecStart=/path/to/the/<executable_name>
# serviceOption2=value2
# serviceOption3=value3

[Install]
# installOption1=value1
# installOption2=value2

[Install] section can be skiped. Do skip it when creating a systemd.timer.

Step 3. Reload unit changes from disk

For systemd to reload the added/modified units files use:

systemctl daemon-reload

Step 4. Enable and start the service

To enable the service use

systemctl enable <service_name>

To start the service use

systemctl start <service_name>
being <service_name> the name of the service.

Example 1. Save the current timestamp each minute

Steps

First, create the folder:

mkdir -p /usr/local/bin

Then, copy the files:

/usr/local/bin/timestamp_each_minute.sh
#!/bin/bash
while true; do
    echo $(date) >> /home/root/example.log
    sleep 1m
done
/lib/systemd/system/timestamp_each_minute.service
[Unit]
Description=Service that writes the date every minute

[Service]
ExecStart=/usr/local/bin/timestamp_each_minute.sh

[Install]
WantedBy=multi-user.target

Give /usr/local/bin/timestamp_each_minute.sh execute permission

chmod +x /usr/local/bin/timestamp_each_minute.sh
Then reload and start.
systemctl daemon-reload
systemctl start timestamp_each_minute.service

How to create a systemd.timer

Official systemd.timer documentation.

To create a systemd.timer it is important to create it as a Linux service. To do so, three files are needed:

  • executable file
  • service file
  • task file

Step 1. Create the service and executable files

Follow Steps 1 and 2 of How to create a systemd.service.

Step 2. Create the timer unit file

Create a timer unit file <service_name>.timer in /lib/systemd/system/ with

nano /lib/systemd/system/<service_name>.timer
where <service_name> must be the name of the <service_name>.service created in Step 1.

This file should contain three sections:

[Unit]
# unitOption1=value1
# unitOption2=value2

[Timer]
# serviceOption1=value1
# serviceOption2=value2

[Install]
# installOption1=value1
# installOption2=value2

Step 3. Reload unit changes from disk

For systemd to reload the added/modified units use:

systemctl daemon-reload

Step 4. Activate and start the timer

To enable the service use

systemctl enable <service_name>.timer

To start the service use

systemctl start <service_name>.timer

Example 1. Save the current timestamp each minute

Steps

First, create the folder:

mkdir -p /usr/local/bin

Then, copy the files:

/usr/local/bin/timestamp_each_minute.sh
#!/bin/bash
echo $(date) >> /home/root/example.log
/lib/systemd/system/timestamp_each_minute.service
[Unit]
Description=Service that writes the date every minute

[Service]
ExecStart=/usr/local/bin/timestamp_each_minute.sh
/lib/systemd/system/timestamp_each_minute.timer
[Unit]
Description=Runs every 1 minutes the timestamp_each_minute.service

[Timer]
OnCalendar=*:0/1

[Install]
WantedBy=timers.target

Give /usr/local/bin/timestamp_each_minute.sh execute permission

chmod +x /usr/local/bin/timestamp_each_minute.sh
Then reload and start.
systemctl daemon-reload
systemctl start timestamp_each_minute.timer