The 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

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.

For more detailed information about the systemctl command, please check here.


How to use journalctl to view and manipulate systemd logs

The journald daemon is the 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.

For more detailed information about the journalctl command, please check here.


Timesyncd

It 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.

For more detailed information about the timesyncd daemon, please check here.


Resolved

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.

For more detailed information about the resolvectl command, please check here.


How to create a new systemd service in the eManager

To create a new systemd service in the eManager, 2 different files are needed: the service file and the executable file. To do so, you have to replicate the following steps:

Step 1. Create a configuration file for the service

Create a file in the following path:

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

The content of the created file must be similar to the following one:

[Unit] Description=This is a test that writes the current time every minute in a specific file

[Service] Type=oneshot ExecStart=/bin/bash /usr/local/bin/<executable_name>.sh

Note

If you are working with timers, the .service does not require an [Install] section as it is the timer units that are enabled.

Step 2. Create an executable file

You have to access the following path:

/usr/local/bin

Note

You may have to create the /local/bin directory inside the /usr directory if it is not created yet.

Once you are in the specified path, you have to create a file named <executable_name>.sh. An example of the content of that file could be the following:

#!/bin/bash echo $(date) >> /home/root/<log_name>.log

This example saves the current timestamp each minute in the root /home/root/<log_name>.log.

Once written the desired code in the file, you have to turn this file into an executable using:

chmod +x <executable_name>.sh

Step 3. Reload unit changes from disk

Finally, it is needed to reload the unit changes from disk to systemd using:

systemctl daemon-reload

Step 4. Activate and start the service

To activate the service you can use:

systemctl enable <service_name>

To start the service you can use:

systemctl start <service_name>

Being <service_name> the name of the service that you want to activate and start.

For more information of systemd.service, please check here.


How to create a systemd.timer

To create a systemd.timer it is important to create it as a Linux service. To do so, 3 different files are needed: the service file, the task file and the executable file.

To create a systemd.timer you have to follow these steps:

Step 1. Create the service and executable file

To do so, you can follow the steps detailed here.

Step 2. Create the timer unit file

With our service created and working we can now create our timer. You have to create the timer unit file <timer_unit_name>.timer in /lib/systemd/system/, and add the following:

[Unit] Description=Runs every 1 minutes the <executable_name>.sh

[Timer] OnCalendar=*:0/1

[Install] WantedBy=timers.target

Step 3. Reload unit changes from disk

Finally, it is needed to reload the unit changes from disk to systemd using:

systemctl daemon-reload

Step 4. Activate and start the timer

To activate the service you can use:

systemctl enable <timer_unit_name>.timer

To start the service you can use:

systemctl start <timer_unit_name>.timer

Important Note

You must name the timer the same as the service but with a .timer extension, unless you define the unit service name with the Unit= option.

If you want more information about systemd.timer service, please check here.