07 - Service Management with SYSTEMD
SYSTEMD and Services
What is SYSTEMD?
Systemd is a Linux initialization system and service manager that is responsible for bootstrapping the system, managing system processes, and providing a standardized approach to manage services, tasks, and logs.
Systemd replaced older init systems like SysVinit and Upstart in most modern Linux distributions due to its efficiency and enhanced features.
All the major distributions, such as RHEL, CentOS, Fedora, Ubuntu, Debian, adopted
systemd
as their init system.
What is a service unit?
A service unit file is a file with the
.service
extension contains information about a process which is managed by systemd.This needs to be created at
/etc/systemd/system/<service-name>.service
location.With systemd, the service events are automatically logged (without any additional configuration).
It is composed by three main sections:
Unit (optional)
Unit
: This section contains the description of the unit itself, and information about its behavior and its dependencies: (to work correctly a service can depend on another one). It consists of following options (few of many):$ cat /etc/systemd/system/project-mercury.service [Unit] Description=Python Django for Project Mercury # description of the unit/service Documentation=http://wiki.caleston-dev.ca/mercury # details of the service and documentation related to it After=postgresql.service # use to specify that our unit/service should be started after the units we provide in the form of a space-separated list
Service (required)
Service
: In this section, we can specify things as the command to be executed when the service is started, or the type of the service itself.[Service] ExecStart=/bin/bash /usr/bin/project-mercury.sh # script to start User=project_mercury # service accoutn to be used to start the service instead of root Restart=on-failure # defines how and when to restart the service RestartSec=10 # to set the time in seconds to wait before the system attempt to restart the service
Install (optional)
Install
: This section contains information about the installation of the unit. This allow the service to be enabled during boot.[Install] WantedBy=graphical.target # This allow the service to be enabled during boot
Commands
Start a service:
$ systemctl start <service_name>
Stop a service:
$ systemctl stop <service_name>
Restart a service:
$ systemctl restart <service_name>
Enable a service to start at boot:
$ systemctl enable <service_name>
Disable a service from starting at boot:
$ systemctl disable <service_name>
Check the status of a service:
$ systemctl status <service_name>
Reload service configuration without stopping it:
$ systemctl reload <service_name>
Reload systemd to recognize new or modified unit files:
$ systemctl daemon-reload
SYSTEMD Tools
- SYSTEMD consists of 2 major tools i.e.
systemctl
andjournalctl
.
SYSTEMCTL
Systemctl is the main command used to manage services on a
SYSTEMD
managed server.It can be used to manage services such as
START/STOP/RESTART/RELOAD
as well asENABLE/DISABLE
services during the system boot.It is also used to
LIST AND MANAGE UNITS
andLIST AND UPDATE TARGETS
.
Commands
systemctl start <service-name>
: To start a service, for example to start a docker service:$ systemctl start docker
systemctl stop <service-name>
: To stop a service, for example to stop a docker service:$ systemctl stop docker
systemctl restart <service-name>
: To restart a service, this will stop and start the service again.$ systemctl restart docker
systemctl reload <service-name>
: To reload a service, this will reload all the configuration without interrupting the normal functionality of the service.$ systemctl reload docker
systemctl enable <service-name>
: To enable a service and make it persistent accross reboots.[~]$ systemctl enable docker
systemctl disable <service-name>
: To disable a service at boot.[~]$ systemctl disable docker
systemctl status <service-name>
: To know the status of the service, this command provided the state of the service. If running properly is should showactive (running)
state.[~]$ systemctl status docker
systemctl deamon-reaload
: To reload systemd to recognize new or modified unit/service files, it reloads the system manager configuration and makes the systemd aware of the changes.$ systemctl daemon-reload
systemctl edit <service-file> --full
: To edit the service file, this will open a text editor, you can make the changes and re-write the settings as needed, making changing this way applied immediately without running thesystemctl daemon reload
command.$ systemctl edit project-mercury.service --full
systemctl get-default
: To see the current runlevel.$ systemctl get default
systemctl set-default <target-name>
: To change the runlevel to a different target.$ systemctl set-default multi-user.target
systemctl list-units --all
: To list all the units that systemd has loaded, this lists all the unit which are active, inactive or another state.$ systemctl list-units --all
systemctl list-units
: To list only active units.$ systemctl list-units
systemctl cat <service-name>
: To view, and also locate a unit file, a comment line containing the path to the unit file is printed as the first line of output.$ systemctl cat project-mercury.service
JOURNALCTL
Journalctl is a command for quering/viewing logs collected by systemd.
The systemd-journald service is responsible for systemd’s log collection, and it retrieves messages from the kernel systemd services, and other sources.
Very useful when you are troubleshooting issues with systemd services.
Commands
journalctl
: To print all the log entries from oldest to the newest.$ journalctl
journalctl -b
: To print all the logs from the current boot.$ journalctl -b
journalctl -u <service-name/unit-name>
: To print all the logs specific to the unit specified.$ journalctl -u docker.service
journalctl -u docker.service --since <time>
: To print all the logs specific to the unit specified since the given time.[~]$ journalctl -u docker.service --since "2022-01-01 13:45:00"