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:
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.
|