📜 ⬆️ ⬇️

Cheat Sheet Management Services CentOS 7 with systemd

Systemd is a system and services manager in the Linux operating system. When developing, they sought to design backwards compatible with the SysV init initialization scripts and provide useful features, such as parallel launch of system services at boot time, activation of daemons on demand, support for system state snepshots, and dependency-based service management logic. On CentOS 7, systemd replaces Upstart as the default init system.

In this article we will look at the process of managing services in systemd for a CentOS 7 user. This knowledge will also be useful in other distributions, because systemd has been used in Fedora for a long time and is planned in Ubuntu 14.10 and Debian 8. Good or not, we'll leave it behind the scenes.

CentOS 7 Systemd Infobox
')
In the process of reading the article, you can try systemd on classic VPS and cloud VPS from Infobox. We strive to add support for modern operating systems in a timely manner so that you can use the latest technologies for more efficient operation. The very idea of ​​writing the article was born after another user question about the use of services in CentOS 7.

Introduction


Systemd brings the concept of systemd units. Units are represented by configuration files located in one of the directories:

Units contain information about system services, listening sockets, saved snapshots of system states and other objects related to the initialization system.

Systemd unit types:


The main systemd functions in CentOS 7




Service Management


In previous versions of CentOS, SysV or Upstart was used. Initialization scripts were located in the /etc/rc.d/init.d/ directory. Such scripts were usually written on Bash and allowed the administrator to manage the state of services and daemons. In CentOS 7, the initialization scripts were replaced by service units.

By the way of using, service units .service resemble initialization scripts. To view, start, stop, restart, enable or disable system services, use the systemctl command. The service and chkconfig commands are still included in the system, but only for compatibility reasons.


When using systemctl, it is not necessary to specify the file extension.

The following are the main systemctl commands :


We work with targets (targets) Systemd


Previous versions of CentOS with SysV init or Upstart included a predefined set of launch levels (runlevels) that represented specific modes for operations, numbered from 0 to 6. In CentOS 7, the concept of launch levels was replaced by systemd targets.

The systemd .target target files are intended to group together other systemd units through a chain of dependencies. For example, the graphical.target unit, used to start a graphics session, starts the GNOME Display Manager ( gdm.service ) and Accounts Service ( accounts – daemon.service ) system services and activates multi-user.target . In turn, multi–user.target launches other system services, such as Network Manager ( NetworkManager.service ) or D-Bus ( dbus.service ), and activates other target units of basic.target .

CentOS 7 has predefined targets, similar to the standard set of launch levels. For compatibility reasons, they also have aliases for these purposes, which are directly displayed in the SysV launch levels.


The runlevel and telinit commands are still available, but left on the system for compatibility reasons. It is recommended to use systemctl to modify or configure system targets.

To determine which target unit is used by default, the following command is useful: systemctl get – default .

To view all loaded target units, use the systemctl list-units - type target command , and to view all target units in general with the command: systemctl list-units --type target --all .

To change the default target, use the systemctl command set-default name.target .

To change the current target: systemctl isolate name.target . The team will launch the target unit and all its dependencies and immediately stop all the others.

Shutting down and rebooting the system


On CentOS 7, the systemctl replaces a significant number of power management commands. The former commands are kept for compatibility, but it is recommended to use systemctl:
systemctl halt - stops the system
systemctl poweroff - shuts down the system
systemctl reboot - reboot the system

Managing systemd on a remote machine


Systemd allows you to control a remote machine via SSH. To manage, use the command:
systemctl --host user_name @ host_name command , where user_name is the user name, host_name is the name of the host that is being remotely managed, and command is the executable systemd command.

Typical systemd .service


This section will help you if you need to quickly make support for service management from systemd. Detailed information on all the parameters of the .service file is in the corresponding section of the systemd documentation.

[Unit] Description=Daemon to detect crashing apps After=syslog.target [Service] ExecStart=/usr/sbin/abrtd Type=forking [Install] WantedBy=multi-user.target 

Let's take a look at the [Unit] section. It contains general information about the service. This section is not only in service units, but also in other units (for example, when controlling devices, mount points, etc.). In our example, we give a description of the service and indicate that the daemon should be running after Syslog.

The next section [Service] directly contains information about our service. The ExecStart parameter used points to the executable file of our service. In Type, we specify how the service notifies systemd when the launch is complete.

The final [Install] section contains information about the target for which the service should start. In this case, we say that the service should be started when the goal of multi–user.target is activated.

This is the minimum operating systemd service file. After writing your own, for testing, copy it into /etc/systemd/system/service_service.service. Run the systemctl daemon-reload commands. Systemd finds out about the service and you can start it.

Additional Information


An excellent systemd guide from RedHat that forms the basis of this article.
Documentation on writing your systemd service unit .
"Systemd for administrators" from the systemd developer in Russian.

Conclusion


In this article, we learned how to manage CentOS 7 services. Of course, this is not the only systemd function and its other sides will be considered in the future. OS itself almost from the time of release is available on classic VPS and cloud VPS from Infobox. Try systemd right now. This knowledge will be useful in connection with the transition of many distributions to systemd.

If you find an error in the article, the author will gladly correct it. Please write in the LAN or in the mail about it.
In case you cannot post comments on Habré, you can write them in the InfoboxCloud Community blog or in our group on Facebook .

Successful use of CentOS 7!

Source: https://habr.com/ru/post/241237/


All Articles