Systemd Tutorial: Practical Guide

< Systemd 入门教程:命令篇

Systemd Tutorial: Practical Guide

Author: Ruan Yifeng

Date: March 8, 2016

In the previous article, I introduced Systemd's Main commands Today, I'll introduce how to use it to complete some basic tasks. bg2016030801

One, boot up

For software that supports Systemd, during installation, it will automatically be set up to run on: /usr/lib/systemd/system Add a configuration file to the directory. If you want the software to start up on boot, run the following command: httpd.service (e.g.).

The above command is equivalent to /etc/systemd/system Add a symbolic link to the catalog pointing to /usr/lib/systemd/system inside httpd.service File. This is because when the computer boots up, Systemd Only execute /etc/systemd/system Configuration files in the directory. This means that placing the modified configuration file in this directory will override the original configuration.

2. Start Service

After setting up to boot, the software will not launch immediately; it must wait until the next restart. If you want to run the software now, you need to execute systemctl start Command. After setting up to boot automatically, the software will not start immediately; it must wait for the next startup. If you want to run the software now, you must execute systemctl start Command.

After executing the above command, there may be a failure to start, so it is necessary to... systemctl status Command to check the status of the service.

The above output result means as follows.

Loaded line: Configuration file location, set to startup with system? Active row: indicates running Main PID line: Main Process ID Status line: Current software status provided by the application itself (here httpd) CGroup block: all child processes of the application Log block: application logs

Section 3: Cease Service

Terminate a running service requires executing systemctl stop Command.

有时候,该命令可能没有响应,服务停不下来。这时候就不得不"杀进程"了,向正在运行的进程发出 kill Signal.

In addition, to restart the service, one must execute systemctl restart Command.

Understanding the Configuration File

How to start a service is entirely determined by its configuration file. Let's take a look at what the configuration file contains.

As mentioned earlier, the configuration files are mainly placed /usr/lib/systemd/system Contents, possibly also in /etc/systemd/system Table of contents. Once the configuration file is found, open it with a text editor. systemctl cat Commands can be used to view configuration files, below is an example of how to do this. sshd.service For example, the file's function is to launch an SSH server for other users to log in via SSH.

The configuration file is divided into several sections, each containing a number of key-value pairs.

The following explains the content of each block in turn.

Five: [Unit] Block: Start-up sequence and dependencies.

Unit Block's Description The field provides a brief description of the current service. Documentation Field provides document location. Subsequent settings are launch order and dependencies, which are quite important.

After Field: indicates if network.target or sshd-keygen.service Need to start, then. sshd.service Start them after these.

Correspondingly, there is also a Before Field, definition sshd.service Should be started before which services. Note, After With Before Fields only relate to startup sequence, not dependencies.

For example, a web application requires a PostgreSQL database to store data. In the configuration file, it is only defined to start after PostgreSQL, but not to depend on PostgreSQL. After going live, if PostgreSQL needs to be restarted for some reason, the web application will be unable to establish a database connection during the downtime. To set up dependencies, you need to use Wants Fields and Requires Field.

Wants Field: Indicates sshd.service With sshd-keygen.service 之间存在"弱依赖"关系,即如果"sshd-keygen.service"启动失败或停止运行,不影响 sshd.service Continue.

Requires 字段则表示"强依赖"关系,即如果该服务启动失败或异常退出,那么 sshd.service Also must withdraw.

Note that, Wants Field and Requires Fields only concern dependency, not launch order; they start concurrently by default.

Six. [Service] Block: Launch Behavior

Service Block defines how to start the current service.

6.1 Launch command

Many software has its own environment parameter file, which can be used EnvironmentFile Field reading.

EnvironmentFile Field: Specifies the environment parameter file for the current service. The file contains: key=value key-value pairs, can be used $key In the form, obtained from the current configuration file.

In the above example, the sshd's environment parameter file is /etc/sysconfig/sshd The most important field in the configuration file is: ExecStart

ExecStart Field: Definition of the command executed when starting a process.

In the above example, the startup... sshd The executed command is: /usr/sbin/sshd -D $OPTIONS in which the variable $OPTIONS From... EnvironmentFile Field specified environment parameter file. Fields with similar functions include the following.

ExecReload Field: Command executed during service restart

ExecStop Field: Command executed upon stopping service

ExecStartPre Field: Command executed before starting the service

ExecStartPost Field: Command executed after service startup

ExecStopPost Field: Command executed after stopping the service

Please refer to the examples below.

The above configuration file, second line ExecStart Set to null, it cancels the settings of the first row. The running result is as follows.

Before all startup settings, a hyphen can be added. - ),表示"抑制错误",即发生错误的时候,不影响其他命令的执行。比如, EnvironmentFile=-/etc/sysconfig/sshd (As an aside, the em dash following the equals sign) indicates even though. /etc/sysconfig/sshd File does not exist; no error will be thrown.

6.2 Startup Type

Type Field definition for startup type. The possible values it can be set to are as follows.

simple (default): ExecStart The field starts the process as the main process.

forking: ExecStart Field will be fork() Mode launch, at this point, the parent process will exit, and the child process will become the main process.

oneshot: similar to simple But only executes once, systemd waits for it to complete before starting other services.

dbus: akin to simple But will start after waiting for D-Bus signals.

notify: akin to simple Notifications will be sent upon completion, and then Systemd will start other services.

idle: akin to simple However, the service will only be initiated after all other tasks are completed. One use case is to prevent the output of this service from being mixed with that of other services.

The configuration file above, the startup type is set to oneshot This indicates that the service needs to be run only once and does not require long-term operation. If you want to reopen it in the future, modify the configuration file as follows.

In the above configuration file, RemainAfterExit Field set to yes After the process exits, the service remains running. In this case, once used... systemctl stop Order to stop the service. ExecStop The specified command will execute, thereby restarting the touchpad.

6.3 Reboot behavior

Service Blocks contain some fields that define the restart behavior.

KillMode Field: Define how Systemd stops the sshd service.

In the above example, the translation is: "In the above example, the translation is:" KillMode Set as process , indicates stopping only the main process, not any sshd child processes, i.e., child processes' open SSH sessions remain connected. This setting is uncommon but crucial for sshd, otherwise, when you stop the service, it will also terminate your own open SSH session.

KillMode Field values can be set as follows.

control-group (default): All child processes within the current control group will be killed

process: terminate only the main process

mixed: The main process will receive a SIGTERM signal, while the child process will get a SIGKILL signal.

No processes will be terminated; only the service's stop command is executed.

Next is Restart Field.

Restart Field: Defines the restart method for systemd after sshd exits.

In the above example, Restart Set as on-failure , any unexpected failure will cause sshd to restart. If sshd stops normally (e.g., executing systemctl stop Command), it will not reboot.

Restart Field values can be set as follows.

no (default): No reboot after exit

on-success: Restart only when exited normally (exit code 0)

on-failure: Restart only on abnormal exit (non-zero exit code), including termination by signal and timeout.

on-abnormal: Restart only on signal termination and timeout.

on-abort: Restart only on uncaught signal termination

on-watchdog: Exit after timeout, then restart

时候 Always: Regardless of the reason for quitting, always restart at this point.

For daemons, it is recommended to set as: on-failure For services that allow for error exits, it can be set as: on-abnormal

Lastly is RestartSec Field.

RestartSec Field: The number of seconds to wait before Systemd restarts the service. The example above sets it to wait for 42 seconds.

Seven, [Install] Block

Install Block, define how to install this configuration file, i.e., how to boot it up.

WantedBy Field: Represents the Target where the service is located.

Target The meaning is service group, indicating a set of services. WantedBy=multi-user.target It refers to the target where sshd is located. multi-user.target This setting is very important because of the execution. systemctl enable sshd.service When issuing commands, sshd.service a symbolic link, it will be placed /etc/systemd/system Under the table of contents multi-user.target.wants In the subdirectory, Systemd has a default startup Target.

The result above indicates that the default launch target is multi-user.target All services in this group will start on boot. That's why. systemctl enable The reasons why commands can be set to boot up. When using Targets, systemctl list-dependencies Orders and systemctl isolate Command is also very useful.

Generally, there are two commonly used Targets: one is... multi-user.target , indicates multi-user command-line status; the other is graphical.target Graphical User State, it depends on multi-user.target The official document includes a very clear image. Dependency diagram of Target

Chapter 8: Target Configuration File

Target also has its own configuration file.

Note, there is no startup command in the Target configuration file. The main fields in the above output are as follows.

Requires Field: Requirement basic.target Run together.

Conflicts 名:冲突名称。 Field: Conflict Field. If rescue.service or rescue.target Running. multi-user.target Cannot run, vice versa.

After ": signifies" multi-user.target In basic.target rescue.service rescue.target Later initiate, if they have any to initiate.

AllowIsolate Permitted to use systemctl isolate Command to switch to multi-user.target

Restart after modifying the configuration file.

After modifying the configuration file, you need to reload the configuration file and then restart the relevant services.

(End)

Document Information

Copyright Statement: Free for non-commercial redistribution - No derivatives - Keep attribution Creative Commons 3.0 License

Date of Publication: Mar. 8, 2016

You recently used:

收藏: favorite Menu QQ