10.7. 管理服务时的其他注意事项
在正常操作过程中,systemd 维护单元抽象和系统上活动的基础进程之间的关联。
From: man systemd
Processes systemd spawns are placed in individual Linux control groups named after the unit which they belong to in the private systemd hierarchy. (see cgroups.txt[1] for more information about control groups, or short "cgroups"). systemd uses this to effectively keep track of processes. Control group information is maintained in the kernel, and is accessible via the file system hierarchy (beneath /sys/fs/cgroup/systemd/), or in tools such as ps(1) (ps xawf -eo pid,user,cgroup,args is particularly useful to list all processes and the systemd units they belong to).
Processes systemd spawns are placed in individual Linux control groups named after the unit which they belong to in the private systemd hierarchy. (see cgroups.txt[1] for more information about control groups, or short "cgroups"). systemd uses this to effectively keep track of processes. Control group information is maintained in the kernel, and is accessible via the file system hierarchy (beneath /sys/fs/cgroup/systemd/), or in tools such as ps(1) (ps xawf -eo pid,user,cgroup,args is particularly useful to list all processes and the systemd units they belong to).
cgroup 层次结构对于 systemd 的进程和服务健康状况视图至关重要。当进程分叉本身时,它将继承创建进程的 cgroup。在这种情况下,可以通过读取适用的 cgroup.procs 文件的内容来验证与给定单元关联的所有进程,例如:
输出与 systemctl status 单元
操作期间返回的 CGroup 信息匹配:
要直接查看系统范围内进程的这些分组,可以使用 systemd-cgls
实用程序:
要使 systemd 正常工作,必须通过 systemd 系统启动或停止服务,以维护正确的进程进行单元分组。任何执行外部操作的操作都会导致不创建所需的 cgroup 结构。这是因为 systemd 不知道正在启动的进程的特殊性质。
作为上述约束的示例,停止 httpd
服务,然后直接发布 /usr/sbin/httpd
会导致以下结果:
请注意,httpd
进程现在在 user-0.slice 和 session-168.scope 下可见。此服务被视为启动的进程,而非系统服务,后者应直接监控和管理。由于这种错误对齐可能会发生一些故障,包括但不限于:
- 在系统关闭或重启事件期间,服务无法正确关闭。
- 用户注销期间会发送意外信号,如 SIGHUP 和 SIGTERM。
-
带有
Restart=
指令时,不会自动重启失败的进程
非大型应用程序关闭事件可能会导致大量后续应用程序故障,如客户端故障、数据丢失和磁盘损坏。