07 - Service Management with SYSTEMD
I am an aspiring DevOps Engineer proficient with containers and container orchestration tools like Docker, Kubernetes along with experienced in Infrastructure as code tools and Configuration as code tools, Terraform, Ansible. Well-versed in CICD tool - Jenkins. Have hands-on experience with various AWS and Azure services. I really enjoy learning new things and connecting with people across a range of industries, so don't hesitate to reach out if you'd like to get in touch.
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
systemdas their init system.
What is a service unit?
A service unit file is a file with the
.serviceextension contains information about a process which is managed by systemd.This needs to be created at
/etc/systemd/system/<service-name>.servicelocation.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.
systemctlandjournalctl.
SYSTEMCTL
Systemctl is the main command used to manage services on a
SYSTEMDmanaged server.It can be used to manage services such as
START/STOP/RESTART/RELOADas well asENABLE/DISABLEservices during the system boot.It is also used to
LIST AND MANAGE UNITSandLIST AND UPDATE TARGETS.
Commands
systemctl start <service-name>: To start a service, for example to start a docker service:$ systemctl start dockersystemctl stop <service-name>: To stop a service, for example to stop a docker service:$ systemctl stop dockersystemctl restart <service-name>: To restart a service, this will stop and start the service again.$ systemctl restart dockersystemctl reload <service-name>: To reload a service, this will reload all the configuration without interrupting the normal functionality of the service.$ systemctl reload dockersystemctl enable <service-name>: To enable a service and make it persistent accross reboots.[~]$ systemctl enable dockersystemctl disable <service-name>: To disable a service at boot.[~]$ systemctl disable dockersystemctl 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 dockersystemctl 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-reloadsystemctl 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 reloadcommand.$ systemctl edit project-mercury.service --fullsystemctl get-default: To see the current runlevel.$ systemctl get defaultsystemctl set-default <target-name>: To change the runlevel to a different target.$ systemctl set-default multi-user.targetsystemctl 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 --allsystemctl list-units: To list only active units.$ systemctl list-unitssystemctl 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.$ journalctljournalctl -b: To print all the logs from the current boot.$ journalctl -bjournalctl -u <service-name/unit-name>: To print all the logs specific to the unit specified.$ journalctl -u docker.servicejournalctl -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"