Skip to main content

系统和服务管理

系统和服务管理

在统信服务器操作系统中默认使用systemd作为系统守护进程管理工具。systemd并不是一个命令,而是一组命令,涉及到系统管理的方方面面。systemctl是systemd的主命令,主要负责控制systemd系统和服务管理器,可用于查看系统状态和管理系统及服务。

Unit分类

systemd可以管理所有系统资源。不同的资源统称为unit(单元),unit一共分成12种。

systemd单元分类

Unit分类 描述
Service 系统服务,守护进程的启动、停止、重启和重载是此类unit中最为明显的几个类型。
Target 多个unit构成的一个组。
Device 硬件设备,此类unit封装一个存在于Linux设备树中的设备。每一个使用udev规则标记的设备都将会在systemd中作为一个设备unit出现。udev的属性设置可以作为配置设备unit依赖关系的配置源。
Mount 文件系统挂载点,此类unit封装系统结构层次中的一个挂载点。
Automount 自动挂载点,此类unit封装系统结构层次中的一个自挂载点。每一个自挂载unit对应一个已挂载的挂载unit。
Path 文件或路径,用于监控指定文件或目录变化,并触发其他unit运行。
Scope 不是由Systemd启动的外部进程。
Slice 进程组。
Snapshot systemd快照,可以切回unit的某个运行状态。
Socket 进程间通信的socket,此类unit封装系统和互联网中的一个socket。当下,systemd支持流式、数据报和连续包的AF_INET、AF_INET6、AF_UNIX socket,也支持传统的FIFOs传输模式。
Swap swap文件。
Timer 定时器。

:::warning

每个配置单元都有一个对应的配置文件,系统管理员的任务就是编写和维护这些不同的配置文件,比如一个MySQL服务对应一个“mysql.service”文件。 :::

Unit基础操作

■执行systemctl --help命令,查看其他更多参数及其含义。

■执行systemctl --version命令,检查您的系统中是否安装有systemd并查询当前安装的版本。

■执行systemctl reboot命令,重启系统。

■执行systemctl poweroff命令,关闭系统、切断电源。

■执行systemctl halt命令,CPU停止工作。

■执行systemctl hybrid-sleep命令,让系统进入交互式休眠状态。

■执行systemctl rescue命令,启动进入救援状态(单用户状态)。

Unit启动耗时

■执行systemd-analyze命令,用于查看启动耗时。

■执行systemd-analyze blame命令,查看每个服务的启动耗时。

■执行systemd-analyze critical-chain命令,显示瀑布状的启动过程流。

[root@Localhost -]# systemd-analyze critical-chain
The time when unit became active or started is printed after the"" character.
The time the unit took to start is printed after the "" character.
graphical.target @4.337s
multi-user.target @4.337s
,service @3.946s+2.369s
tuned.service 3.202s+740ms
Lnetwork.target @3.192s
LNetworkManager.service @3.126s+65ms
Lnetwork-pre.target@3.125s
Lfirewalld.service @1.942s +1.183s
LpoLkit.service @2.011s+176ms
L
-basic.target @1.872s
-sockets.target @1.872s
Lsssd-kcm.socket @1.872s
-sysinit.target @1.870s
Lsys-fs-fuse-connections.mount @7.461s+27ms
systemd-journald.socket
Lsystem.slice
L-.slice
[root@Localhost-]#

Unit查看

systemctl list-units命令可以查看当前系统的所有unit。

■执行systemctl list-unit-files命令,列出所有可用单元。

■执行systemctl list-units命令,列出所有运行中单元。

■执行systemctl --failed命令,列出所有失败单元。

■执行systemctl list-units --all --state=inactive命令,列出所有没有运行的单元。

■执行systemctl list-units --type=service命令,列出所有正在运行的、类型为service的单元。

Unit状态

systemctl status命令用于查看系统状态和单个unit的状态。

■执行systemctl status命令,显示系统状态。

■执行systemctl status dbus.service命令,显示单个unit的状态。

■执行systemctl is-active dbus.service命令,查看某个unit是否运行。

[root@localhost ~]# systemctl is-active dbus.service

active

[root@localhost ~]# systemctl is-active application.service

inactive

Unit 管理

对于您来说,最常用的是或许是以下这些命令,用于启动或停止unit(主要针对service类型的unit)。

■执行systemctl start apache.service命令,立即启动一个服务。

■执行systemctl stop apache.service命令,立即停止一个服务。

■执行systemctl restart apache.service命令,重启一个服务。

■执行systemctl kill apache.service命令,杀死一个服务的所有子进程。

■执行systemctl reload apache.service命令,重新加载一个服务的配置文件。

■执行systemctl daemon-reload命令,重载所有修改过的配置文件。

■执行systemctl enable firewalld.service命令,使某服务自动启动。

■执行systemctl disable firewalld.service命令,使某服务不自动启动。