[Linux] Detailed explanation of Linux system service service, systemd

Foreword:

A service is a program that resides in memory and can provide some network or service functions. It is called service in English.

1. systemd configuration directory

From CentOS 7.x onwards, the Red Hat series gave up initthe way of starting scripts to manage services, and switched to systemd, a startup service management mechanism. The benefits of this mechanism are:

  • Parallel processing services to speed up the boot process, systemd can make all services start at the same time.
  • Service dependency self-check.
  • Classify services according to their functions and group multiple services into one group.

systemd refers to past execution scripts collectively as a service unit (unit). When each service unit is distinguished by function, it is classified into different types (type).

The basic types include (generally distinguished by different suffixes):

  • system service
  • Socket file service (socket) for data monitoring and exchange
  • The type of snapshot that stores system state
  • Provide different operating environments (target) similar to the classification of execution levels

There are three types of systemd configuration directories:

  • /usr/lib/systemd/system/: The most important startup script settings for each service
  • /run/systemd/system/: Service scripts generated during system execution, these scripts have a higher priority than
  • /usr/lib/systemd/system/ high
  • /etc/systemd/system/: The execution script created by the administrator according to the requirements of the host system has a higher execution priority than /run/systemd/system/
  • /etc/systemd/system/

The services that need to be started are stored in the directory. There are a large number of link files in this directory, which are linked to the /usr/lib/systemd/system/ directory. The files in this directory are the service script files actually started by systemd.
insert image description here

2. systemd service unit (unit) classification

As mentioned above, according to different functions, service units are divided into different types, which can be divided into the following types according to the extension of the service script file.
insert image description here
Among them, the system service type of .service is the most common.

3. Use systemctl to manage services

The mechanism for systemd to start services is mainly systemctlhandled by commands.

[root@study ~]# systemctl [command] [unit]
command 主要有:
start :立刻启动后面接的 unit
stop :立刻关闭后面接的 unit
restart :立刻关闭后启动后面接的 unit,亦即执行 stop 再 start 的意思
reload :不关闭后面接的 unit 的情况下,重新载入配置文件,让设置生效
enable :设置下次开机时,后面接的 unit 会被启动
disable :设置下次开机时,后面接的 unit 不会被启动
status :目前后面接的这个 unit 的状态,会列出有没有正在执行、开机默认执行否、登录等信息等!
is-active :目前有没有正在运行中
is-enable :开机时有没有默认要启用这个 unit

3.1 View the current service status

Use systemctl statusthe command to view the status of the course service. The status meanings are as follows:

  • Loaded: This line indicates whether the service will be started at boot, enabled means it will be started at boot, disabled means it will not be started, and vendor preset means the default value when it is installed for the first time.
  • Active: The current status of this service unit is running (running) or not executing (dead)

In addition to running and dead, Active has several other basic states:

  • active (running): means that one or more programs are executing in the system
  • active (exited): The service is executed only once and ends normally, and no program is currently executing in the system.
  • active (waiting): It is being executed, but it is still waiting for other events to continue processing.
  • inactive: The service is not currently running.

The status of Loaded is also:

  • enabled: This service will be executed at boot time
  • disabled: This daemon will not be executed when booting
  • static: This daemon cannot be started by itself (not enable), but it may be enabled by others
  • service to wake up
  • mask: This daemon cannot be started anyway! Because it has been forcibly logged out (not deleted).
    insert image description here

3.2 View existing services in the system

Use list-unitsthe command to view the currently started services and list-unit-filesthe installed services, the command is as follows

[szp@study ~]# systemctl [command] [--type=TYPE] [--all]
command:
list-units :依据 unit 列出目前有启动的 unit。若加上 --all 才会列出没启动的。
list-unit-files :依据 /usr/lib/systemd/system/ 内的文件,将所有文件列表说明。
--type=TYPE:就是之前提到的 unit type,主要有 service, socket, target 等

insert image description here
The systemctl command without parameters is equivalent to systemctl list-units, and the meanings of the columns in the printed results are as follows:

  • UNIT: the name of the project, including the category of each unit
  • LOAD : Whether it will be loaded when booting, the default systemctl shows only loaded items
  • ACTIVE: The current state must be matched with the subsequent SUB! It is the active item when observed with systemctl status
  • DESCRIPTION : Detailed description

Use to systemctl list-unit-fileslist installed services.
insert image description here
Use the following command systemctl list-units –type=service –all to filter services of the service category.
insert image description here

3.3 View dependencies between services

systemctl list-dependencies [unit] [--reverse]
--reverse :反向追踪谁使用这个服务单元

The command execution result is shown in the figure below.
insert image description here

4. systemctl configuration file for service type

Most configuration files of systemd are placed in the /usr/lib/systemd/system/ directory, which stores the default configuration of the software and is not recommended to be modified. The modified configuration files should be placed in the /etc/systemd/system/ directory.

Take vsftpd.service as an example, if you want to modify it, you can do it in the following directory.

  • /usr/lib/systemd/system/vsftpd.service: official default configuration file;
  • /etc/systemd/system/vsftpd.service.d/custom.conf: Create a directory with the same file name as the configuration file under /etc/systemd/system, but add the extension of .d. Then create a configuration file in this directory. The configuration file suffix is ​​preferably .conf. The files in this directory will be attached to /usr/lib/systemd/system/vsftpd.service
  • /etc/systemd/system/vsftpd.service.wants/*: The files in this directory are link files, which set the links of dependent services. It means that after starting vsftpd.service, it is better to start the suggested services under this directory.
  • /etc/systemd/system/vsftpd.service.requires/*: The files in this directory are link files, which set the links of dependent services. It means which services need to be started before starting vsftpd.service.
    insert image description here

Configuration file writing format

Knowing the directory where the configuration file is placed, let's look at the format definition of the configuration file itself.

Take sshd.service as an example, check the content of its file as follows, the file is composed of items indicated by square brackets. Settings are usually repeatable, with later settings superseding previous ones. A blank line or a line beginning with # or ; all represent comments. If setting parameters requires a "yes/no" item (Boolean value, boolean), you can use 1, yes, true, on for on, and 0, no, false, off for off.
insert image description here

  • [Unit]: The description of the unit itself, as well as the settings with other dependent daemons, including setting values ​​such as the service to start the unit after;
  • [Service], [Socket], [Timer], [Mount], [Path]…: Different unit types need to use corresponding setting items. Currently sshd.service is used as an example, so use [Service] to set. Mainly in the standard service startup script, environment configuration file name, restart method and so on.
  • [Install]: This item indicates which target to install this unit into.

The setting details of each item are introduced below.

insert image description here
insert image description here
insert image description here
insert image description here

reference

[Basic entry! ]Linux system service details

Guess you like

Origin blog.csdn.net/m0_45406092/article/details/130588316