Detailed explanation of systemd(service file) of Centos7 and Centos8

1, Power on

For software that supports systemd, a configuration file will be automatically added to the / usr/lib/systemd/system directory during installation.

If you want the software to start, execute the following command (take httpd.service as an example).

systemctl enable httpd
[root@wtl1992 system]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

 

 cat /usr/lib/systemd/system/httpd.service:

# See httpd.service(8) for more information on using the httpd service.

# Modifying this file in-place is not recommended, because changes
# will be overwritten during package upgrades.  To customize the
# behaviour, run "systemctl edit httpd" to create an override unit.

# For example, to pass additional options (such as -D definitions) to
# the httpd binary at startup, create an override unit (as is done by
# systemctl edit) and enter the following:

#       [Service]
#       Environment=OPTIONS=-DMY_DEFINE

[Unit]
Description=The Apache HTTP Server
Wants=httpd-init.service
After=network.target remote-fs.target nss-lookup.target httpd-init.service
Documentation=man:httpd.service(8)

[Service]
Type=notify
Environment=LANG=C

ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
# Send SIGWINCH for graceful stop
KillSignal=SIGWINCH
KillMode=mixed
PrivateTmp=true

[Install]
WantedBy=multi-user.target

The above command is equivalent to adding a symbolic link in the / etc/systemd/system directory to httpd.com in / usr/lib/systemd/system Service file.

This is because systemd only executes the configuration files in the / etc/systemd/system directory when booting up. This also means that if the modified configuration file is placed in this directory, the effect of overwriting the original configuration can be achieved.

2, Start service

After the boot is set, the software will not start immediately and must wait until the next boot. If you want to run the software now, execute the systemctl start command.

systemctl start httpd

After executing the above command, it may fail to start. Therefore, use the systemctl status command to check the status of the service.

Loaded line: the location of the configuration file and whether it is set to startup

Active line: indicates running

Main PID line: main process ID

Status line: the current status of the software provided by the application itself (here is httpd)

Cggroup block: all child processes applied

Log block: applied log

3, Stop service

To terminate a running service, you need to execute the systemctl stop command.

systemctl stop httpd.service

Sometimes, the command may not respond and the service cannot stop. At this time, you have to "kill the process" and send a kill signal to the running process.

systemctl kill httpd.service

In addition, to restart the service, execute the systemctl restart command.

systemctl restart httpd.service

4, Read configuration file

How a service starts is entirely determined by its configuration file. Let's see what the configuration file contains.

As mentioned earlier, the configuration file is mainly placed in / usr/lib/systemd/system directory or / etc/systemd/system directory. After finding the configuration file, open it with a text editor.

The systemctl cat command can be used to view the configuration file. The following is sshd The service file, for example, is used to start an ssh server for other users to log in through SSH.

[root@wtl1992 system]# systemctl cat sshd
# /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.target
Wants=sshd-keygen.target

[Service]
Type=notify
EnvironmentFile=-/etc/crypto-policies/back-ends/opensshserver.config
EnvironmentFile=-/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS $CRYPTO_POLICY
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

As you can see, the configuration file is divided into several blocks, and each block contains several key value pairs.

The contents of each block are explained in turn below.

1. [Unit] block: startup order and dependency

Description: gives a brief description of the current service

Documentation: give the document location.

After: indicates if network Target or sshd keygen If the service needs to be started, sshd Services should start after them.

Before: define sshd Which services should the service start before.

Note that the After and Before fields only relate to the startup order and do not relate to dependencies.

For example, a Web application needs a postgresql database to store data. In the configuration file, it only defines to start after postgresql, and does not define a dependency on postgresql. After going online, postgresql needs to be restarted for some reason. During the service stop, the Web application will not be able to establish a database connection.

To set dependencies, you need to use the Wants field and the Requires field.

Wants: indicates sshd Service and sshd keygen There is a "weak dependency" relationship between services, that is, if "sshd keygen. Service" fails to start or stops running, it will not affect sshd The service continues.

Requires: indicates "strong dependency", that is, if the service fails to start or exits abnormally, sshd The service must also exit.

Note that the Wants field and the Requires field only involve dependencies and have nothing to do with the startup order. They are started at the same time by default.

2. [Service] block: start behavior

The Service block defines how to start the current Service.

2.1 start command

Many software have their own environment parameter file, which can be read with the environment file field.

EnvironmentFile: Specifies the environment parameter file of the current service. The key=value key value pair inside the file can be obtained in the current configuration file in the form of $key.

In the above example, the environment parameter file of sshd is / etc/sysconfig/sshd.

The most important field in the configuration file is ExecStart.

ExecStart: defines the command to execute when starting a process.

In the above example, start sshd and execute the command / usr/sbin/sshd -D $OPTIONS. The variable $OPTIONS comes from the environment parameter file specified in the EnvironmentFile field.

Similar to this function, there are the following fields.

ExecReload: command executed when restarting the service

ExecStop: command executed when the service is stopped

ExecStartPre: command executed before starting the service

ExecStartPost: command executed after starting the service

ExecStopPost: command executed after the service is stopped

Keywords: Linux systemd service

Added by rnintulsa on Fri, 17 Dec 2021 01:26:58 +0200