Systemd Tutorial: Practical Guide One, boot up 2. Start Service Section 3: Cease Service Understanding the Configuration File Five: [Unit] Block: Start-up sequence and dependencies. Six. [Service] Block: Launch Behavior 6.1 Launch command 6.2 Startup Type 6.3 Reboot behavior Seven, [Install] Block Chapter 8: Target Configuration File Restart after modifying the configuration file. Document Information
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.
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.).
$ sudo systemctl enable httpd
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.
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.
x
$ sudo systemctl start httpd
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.
x
$ sudo systemctl status httpd
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
Active: active (running) since 金 2014-12-05 12:18:22 JST; 7min ago
Main PID: 4349 (httpd)
Status: "Total requests: 1; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─4349 /usr/sbin/httpd -DFOREGROUND
├─4350 /usr/sbin/httpd -DFOREGROUND
├─4351 /usr/sbin/httpd -DFOREGROUND
├─4352 /usr/sbin/httpd -DFOREGROUND
├─4353 /usr/sbin/httpd -DFOREGROUND
└─4354 /usr/sbin/httpd -DFOREGROUND
12月 05 12:18:22 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
12月 05 12:18:22 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
12月 05 12:22:40 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
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
Terminate a running service requires executing
systemctl stop
Command.
xxxxxxxxxx
$ sudo systemctl stop httpd.service
有时候,该命令可能没有响应,服务停不下来。这时候就不得不"杀进程"了,向正在运行的进程发出
kill
Signal.
xxxxxxxxxx
$ sudo systemctl kill httpd.service
In addition, to restart the service, one must execute
systemctl restart
Command.
x
$ sudo systemctl restart httpd.service
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.
xxxxxxxxxx
$ systemctl cat sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service
[Service]
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
Type=simple
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
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.
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 ifnetwork.target
orsshd-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: Indicatessshd.service
Withsshd-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.
Service
Block defines how to start the current service.
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.
xxxxxxxxxx
[Service]
ExecStart=/bin/echo execstart1
ExecStart=
ExecStart=/bin/echo execstart2
ExecStartPost=/bin/echo post1
ExecStartPost=/bin/echo post2
The above configuration file, second line
ExecStart
Set to null, it cancels the settings of the first row. The running result is as follows.
xxxxxxxxxx
execstart2
post1
post2
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.
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 befork()
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.
xxxxxxxxxx
[Unit]
Description=Switch-off Touchpad
[Service]
Type=oneshot
ExecStart=/usr/bin/touchpad-off
[Install]
WantedBy=multi-user.target
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.
xxxxxxxxxx
[Unit]
Description=Switch-off Touchpad
[Service]
Type=oneshot
ExecStart=/usr/bin/touchpad-off start
ExecStop=/usr/bin/touchpad-off stop
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
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.
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.
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.
xxxxxxxxxx
$ systemctl get-default
multi-user.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.
xxxxxxxxxx
# List all services included in multi-user.target
$ systemctl list-dependencies multi-user.target
Switch to another target
# shutdown.target refers to shutdown state
$ sudo systemctl isolate shutdown.target
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
。
Target also has its own configuration file.
xxxxxxxxxx
$ systemctl cat multi-user.target
[Unit]
Description=Multi-User System
Documentation=man:systemd.special(7)
Requires=basic.target
Conflicts=rescue.service rescue.target
After=basic.target rescue.service rescue.target
AllowIsolate=yes
Note, there is no startup command in the Target configuration file. The main fields in the above output are as follows.
Requires
Field: Requirementbasic.target
Run together.
Conflicts
名:冲突名称。 Field: Conflict Field. Ifrescue.service
orrescue.target
Running.multi-user.target
Cannot run, vice versa.
After
": signifies"multi-user.target
Inbasic.target
、rescue.service
、rescue.target
Later initiate, if they have any to initiate.
AllowIsolate
Permitted to usesystemctl isolate
Command to switch tomulti-user.target
。
After modifying the configuration file, you need to reload the configuration file and then restart the relevant services.
x
# Reload configuration file
$ sudo systemctl daemon-reload
Reboot related services
$ sudo systemctl restart foobar
(End)
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: