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).
cgroup 层次结构对于 systemd 的进程和服务健康状况视图至关重要。当进程分叉本身时,它将继承创建进程的 cgroup。在这种情况下,可以通过读取适用的 cgroup.procs 文件的内容来验证与给定单元关联的所有进程,例如:
~]# cat /sys/fs/cgroup/systemd/system.slice/httpd.service/cgroup.procs 11854 11855 11856 11857 11858 11859
输出与 systemctl status 单元
操作期间返回的 CGroup 信息匹配:
~]# systemctl status httpd ● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: active (running) since Wed 2019-05-29 12:08:16 EDT; 45s ago Docs: man:httpd(8) man:apachectl(8) Main PID: 11854 (httpd) Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec" CGroup: /system.slice/httpd.service ├─11854 /usr/sbin/httpd -DFOREGROUND ├─11855 /usr/sbin/httpd -DFOREGROUND ├─11856 /usr/sbin/httpd -DFOREGROUND ├─11857 /usr/sbin/httpd -DFOREGROUND ├─11858 /usr/sbin/httpd -DFOREGROUND └─11859 /usr/sbin/httpd -DFOREGROUND May 29 12:08:16 localhost systemd[1]: Starting The Apache HTTP Server... May 29 12:08:16 localhost systemd[1]: Started The Apache HTTP Server.
要直接查看系统范围内进程的这些分组,可以使用 systemd-cgls
实用程序:
~]# systemd-cgls | head -17 ├─1 /usr/lib/systemd/systemd --switched-root --system --deserialize 22 ├─user.slice │ └─user-0.slice │ └─session-168.scope │ ├─ 3049 login -- root │ ├─11884 -bash │ ├─11943 systemd-cgls │ └─11944 head -17 └─system.slice ├─httpd.service │ ├─11854 /usr/sbin/httpd -DFOREGROUND │ ├─11855 /usr/sbin/httpd -DFOREGROUND │ ├─11856 /usr/sbin/httpd -DFOREGROUND │ ├─11857 /usr/sbin/httpd -DFOREGROUND │ ├─11858 /usr/sbin/httpd -DFOREGROUND │ └─11859 /usr/sbin/httpd -DFOREGROUND ├─rhnsd.service
要使 systemd 正常工作,必须通过 systemd 系统启动或停止服务,以维护正确的进程进行单元分组。任何执行外部操作的操作都会导致不创建所需的 cgroup 结构。这是因为 systemd 不知道正在启动的进程的特殊性质。
作为上述约束的示例,停止 httpd
服务,然后直接发布 /usr/sbin/httpd
会导致以下结果:
~]# systemctl stop httpd ~]# /usr/sbin/httpd # systemd-cgls | head -17 ├─1 /usr/lib/systemd/systemd --switched-root --system --deserialize 22 ├─user.slice │ └─user-0.slice │ └─session-168.scope │ ├─ 3049 login -- root │ ├─11884 -bash │ ├─11957 /usr/sbin/httpd │ ├─11958 /usr/sbin/httpd │ ├─11959 /usr/sbin/httpd │ ├─11960 /usr/sbin/httpd │ ├─11961 /usr/sbin/httpd │ ├─11962 /usr/sbin/httpd │ ├─11963 systemd-cgls │ └─11964 head -17 └─system.slice ├─rhnsd.service │ └─3261 rhnsd
请注意,httpd
进程现在在 user-0.slice 和 session-168.scope 下可见。此服务被视为启动的进程,而非系统服务,后者应直接监控和管理。由于这种错误对齐可能会发生一些故障,包括但不限于:
- 在系统关闭或重启事件期间,服务无法正确关闭。
- 用户注销期间会发送意外信号,如 SIGHUP 和 SIGTERM。
-
带有
Restart=
指令时,不会自动重启失败的进程
非大型应用程序关闭事件可能会导致大量后续应用程序故障,如客户端故障、数据丢失和磁盘损坏。