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).
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
cgroup 层次结构对于 systemd 的进程和服务健康状况视图至关重要。当进程分叉本身时,它将继承创建进程的 cgroup。在这种情况下,可以通过读取适用的 cgroup.procs 文件的内容来验证与给定单元关联的所有进程,例如:
~]# cat /sys/fs/cgroup/systemd/system.slice/httpd.service/cgroup.procs
11854
11855
11856
11857
11858
11859
~]# cat /sys/fs/cgroup/systemd/system.slice/httpd.service/cgroup.procs
11854
11855
11856
11857
11858
11859
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
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.
~]# 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.
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
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-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
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
要使 systemd 正常工作,必须通过 systemd 系统启动或停止服务,以维护正确的进程进行单元分组。任何执行外部操作的操作都会导致不创建所需的 cgroup 结构。这是因为 systemd 不知道正在启动的进程的特殊性质。
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
~]# 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
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow