This article is a mirror article of machine translation, please click here to jump to the original article.

View: 10964|Reply: 8

[linux] Linux systemd service manager explained in detail

[Copy link]
Posted on 11/28/2021 10:02:32 AM | | | |
systemd is a set of basic building blocks for Linux systems. It provides a system and service manager that runs and starts the rest of the system as PID 1.

systemd provides aggressive parallelization capabilities, uses sockets and D-Bus activation to start services, provides on-demand start of daemons, uses Linux control groups to track processes, maintains mounts and auto-mount points, and implements well-designed transaction-dependency-based service control logic. systemd supports SysV and LSB init scripts and can replace sysvinit.

Other sections include log daemons, utilities to control basic system configurations such as hostnames, dates, locales, maintaining a list of logged-in users and running containers and virtual machines, system accounts, runtime directories, and settings, as well as managing daemon configurations for simple networks, network time synchronization, log forwarding, and name resolution.

The systemd configuration file exists in the following three folders:         

/etc/systemd/system 存放系统启动的默认级别及启动的unit的软连接,优先级最高。

/run/systemd/system 系统执行过程中产生的服务脚本,优先级次之。

/usr/lib/systemd/system 存放系统上所有的启动文件,优先级最低。
Review:

Linux looks at the log output of the Systemd service
https://www.itsvse.com/thread-10154-1-1.html

Jenkins (4) Add a Linux node and register it as a service
https://www.itsvse.com/thread-10120-1-1.html

CentOS 7 installs the Node exporter
https://www.itsvse.com/thread-9969-1-1.html

Unit profile

A unit file in systemd is a file that encodes information about the various units that systemd can manage, including services, sockets, devices, and so on. This guide focuses on services, in which case the unit file we are working with is the .service file. The varnish.service device configuration file contains information about how systemd should execute, monitor, and manage the varnish daemon.

The [Unit] block is usually the first block of the configuration file and is used to define the metadata of the Unit and how it relates to other Units. Its main fields are as follows.

  • Description: A short description
  • Documentation: The address of the document
  • Requires: Other Units that the current unit depends on, and if they are not running, the current unit will fail to start
  • Wants: Other Units that work with the current Unit, if they are not running, the current Unit will not fail to start
  • BindsTo: Similar to Requires, it specifies a Unit that causes the current Unit to stop running if it exits
  • Before: If the Unit specified in this field is also to be started, it must be started after the current Unit
  • After: If the Unit specified in this field is also to be started, it must be started before the current Unit
  • Conflicts: The unit specified here cannot run at the same time as the current unit
  • Condition... : The conditions that must be met for the current unit to run, otherwise it will not run
  • Assert... : The conditions that must be met for the current unit to run, otherwise the startup failure will be reported

[Install] is usually the last block of the configuration file that defines how to boot and whether to boot up. Its main fields are as follows.

  • WantedBy: Its value is one or more Targets, and when the current Unit is active (enable), the symlink is placed in a subdirectory under the /etc/systemd/system directory with the Target name + .wants suffix
  • RequiredBy: Its value is one or more Targets, and when the current Unit is active, the symlink will be placed in a subdirectory under the /etc/systemd/system directory with the Target name + .required suffix
  • Alias: The alias that the current Unit can use to start
  • Also: When the current unit is activated, other units will be activated at the same time

The [Service] block is used to configure the Service, and only units of the Service type have this block. Its main fields are as follows.

  • Type: Defines the behavior of the process at startup. It has the following values.
  • Type=simple: The default value, execute the command specified by ExecStart to start the main process
  • type=forking: Fork creates a child process from the parent process, which will exit immediately after creation
  • Type=oneshot: A one-time process, Systemd will wait for the current service to exit before continuing to execute
  • Type=dbus: The current service is started via D-Bus
  • type=notify: When the current service is started, Systemd will be notified and continue execution
  • type=idle: The current service will run only if other tasks are executed
  • ExecStart: The command to start the current service
  • ExecStartPre: The command executed before starting the current service
  • ExecStartPost: The command executed after starting the current service
  • ExecReload: The command that is executed when the current service is restarted
  • ExecStop: The command that is executed when the current service is stopped
  • ExecStopPost: Stop the command that is executed when it is served
  • RestartSec: The number of seconds between automatic service restarts the current service
  • Restart: Defines the circumstances under which Systemd will automatically restart the current service, with possible values such as always, on-success, on-failure, on-abnormal, on-abort, on-watchdog
  • TimeoutSec: Defines the number of seconds that Systemd waits before stopping the current service
  • Environment: Specify the environment variable

Systemctl management tool

Program management in CentOS is basically done by the tool systemctl.

Startup Service:

systemctl start name.service

Discontinue service:

systemctl stop name.service

Restart service:

systemctl restart name.service

View Status:

systemctl status name.service

Conditional restart:

If the service was started before, it will be restarted, and if the service is not started, it will not be operated

systemctl try-restart name.service

Reload or restart:

Reload first, and if the reload is unsuccessful, reboot

systemctl reload-or-restart name.service

Reload or conditional restart:

systemctl reload-or-try-restart name.service

Sets whether the service can be set by the user to start the state

systemctl unmask name.service

systemctl mask name.service is prohibited

View the current activation status of the service:

The status return value of the service has started command is 0 The status return value of the command that has not been started is non-0

systemctl is-active name.service

See all the services that have been activated:

-t Specifies the type of unit displayed.

--all displays a more detailed list of information.

-a equivalent --all

systemctl list-units

systemctl list-units -t service

systemctl list-units -t service -a

See all services:

systemctl list-units  -a

View all service statuses:

systemctl list-unit-files

-a 、--all : View the status of all services

-t 、--type: Specifies the type of unit to be viewed

loaded: The configuration file has been loaded, loaded into memory

active(running): Runs that are continuously processed one or more times

active(exited): The one-time configuration is successfully completed

active(waiting): Running, waiting for an event

inactive:不运行

enabled:开机启动

disabled:开机不启动

static:开机不启动,但可被另一个启用的服务激活

Used to list at which operating levels the service is enabled and disabled

ls /etc/systemd/system/*.wants/sshd.service

systemctl list-unit-files --type target --all

To set the service to not start:

systemctl disable service name unit

Check if the service is booted and started:

systemctl is-enabled name.service

Look at service dependencies:

systemctl list-dependencies name.service
systemctl list-depebdencies

Heavy load service:

systemctl daemon-reload

Kill the process:

systemctl kill process name

Resources:

The hyperlink login is visible.
The hyperlink login is visible.
The hyperlink login is visible.
The hyperlink login is visible.





Previous:Linux tests LDAP connectivity using the Curl command
Next:vsftp upload file permission issue
 Landlord| Posted on 12/11/2021 5:35:26 PM |
[Unit]A block is usually the first block of a configuration file that defines the metadata of a Unit and how it relates to other Units. Its main fields are as follows.

Description: A short description
Documentation: The address of the document
Requires: Other Units that the current unit depends on, and if they are not running, the current unit will fail to start
Wants: Other Units that work with the current Unit, if they are not running, the current Unit will not fail to start
BindsTo: Similar to Requires, it specifies a Unit that causes the current Unit to stop running if it exits
Before: If the Unit specified in this field is also to be started, it must be started after the current Unit
After: If the Unit specified in this field is also to be started, it must be started before the current Unit
Conflicts: The unit specified here cannot run at the same time as the current unit
Condition... : The conditions that must be met for the current unit to run, otherwise it will not run
Assert... : The conditions that must be met for the current unit to run, otherwise the startup failure will be reported
[Install]It is usually the last block of the configuration file that defines how to boot and whether to boot up. Its main fields are as follows.

WantedBy: Its value is one or more Targets, and when the current Unit is active (enable), the symlink is placed in a subdirectory under the /etc/systemd/system directory with the Target name + .wants suffix
RequiredBy: Its value is one or more Targets, and when the current Unit is active, the symlink will be placed in a subdirectory under the /etc/systemd/system directory with the Target name + .required suffix
Alias: The alias that the current Unit can use to start
Also: When the current unit is activated, other units will be activated at the same time
[Service]Blocks are used to configure services, and only units of type Service have this block. Its main fields are as follows.

Type: Defines the behavior of the process at startup. It has the following values.
Type=simple: The default value, execute the command specified by ExecStart to start the main process
type=forking: Fork creates a child process from the parent process, which will exit immediately after creation
Type=oneshot: A one-time process, Systemd will wait for the current service to exit before continuing to execute
Type=dbus: The current service is started via D-Bus
type=notify: When the current service is started, Systemd will be notified and continue execution
type=idle: The current service will run only if other tasks are executed
ExecStart: The command to start the current service
ExecStartPre: The command executed before starting the current service
ExecStartPost: The command executed after starting the current service
ExecReload: The command that is executed when the current service is restarted
ExecStop: The command that is executed when the current service is stopped
ExecStopPost: Stop the command that is executed when it is served
RestartSec: The number of seconds between automatic service restarts the current service
Restart: Defines the circumstances under which Systemd will automatically restart the current service, with possible values such as always, on-success, on-failure, on-abnormal, on-abort, on-watchdog
TimeoutSec: Defines the number of seconds that Systemd waits before stopping the current service
Environment: Specify the environment variable


 Landlord| Posted on 12/11/2021 5:37:33 PM |
Type=forking
systemd thinks that the service starts successfully when the service process is forked and the parent process exits. For regular daemons, unless you are sure that this startup method does not meet your needs, you can start with this type. With this startup type, PIDFile= should also be specified so that systemd can track the main process of the service

 Landlord| Posted on 12/15/2021 4:55:14 PM |
EnvironmentFile command

EnvironmentFile is similar to the Environment directive, but reads environment variables from a text file. The text file should contain variable assignments separated by line breaks

example

You can take this environment file and use its variables as follows:



 Landlord| Posted on 1/4/2022 3:56:55 PM |
 Landlord| Posted on 1/5/2022 9:48:21 AM |
docker default service file path /usr/lib/systemd/system/docker.service

 Landlord| Posted on 1/23/2022 10:57:56 PM |
View all service statuses



Query whether a service is powered on, using NFS as an example:

 Landlord| Posted on 2/25/2025 5:01:08 PM |
Bulk start services that start with ABC

 Landlord| Posted on 4/11/2025 3:57:14 PM |
DefaultTimeoutStartSec= and DefaultTimeoutStopSec= default to 90 seconds in System Manager and 90 seconds in User Manager.

The hyperlink login is visible.

View the configuration of the specified service with the following command:


Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com