When working with Linux systems, particularly in containerized environments, knowing how to inspect the cgroup (control group) of a process is crucial for resource management and debugging. Here are several reliable methods to achieve this.
The most straightforward way to check a process's cgroup is through the proc filesystem:
cat /proc/[PID]/cgroup
Example output:
12:memory:/docker/abcdef123456
6:cpuacct,cpu:/docker/abcdef123456
3:blkio:/docker/abcdef123456
Modern versions of ps
(from procps-ng 3.3.0+) support cgroup information:
ps -o cgroup [PID]
Or for more detailed output:
ps xawf -eo pid,user,cgroup,args
For systems using systemd, you can inspect the hierarchy:
systemd-cgls
To find a specific process:
systemd-cgls | grep [PID]
On systems using cgroup v2:
cat /sys/fs/cgroup/[PID]/cgroup.procs
Or to find all processes in a cgroup:
cat /sys/fs/cgroup/unified/cgroup.procs
When debugging a containerized application:
# Find container process
docker ps
docker top [CONTAINER_ID]
# Then check its cgroup
cat /proc/$(docker inspect --format '{{.State.Pid}}' [CONTAINER_ID])/cgroup
Here's a bash function to quickly check cgroups:
get_cgroup() {
if [ -z "$1" ]; then
echo "Usage: get_cgroup [PID]"
return 1
fi
echo "Cgroup information for PID $1:"
cat "/proc/$1/cgroup"
}
When working with Linux process management, you'll often need to inspect which control groups (cgroups) a particular process belongs to. This information becomes crucial for resource monitoring, container orchestration, and system debugging.
The most straightforward method is to examine the process's entry in the /proc
filesystem:
cat /proc/[PID]/cgroup
For example, to check cgroups for process ID 1234:
cat /proc/1234/cgroup
On systems using systemd (most modern distributions), you can use:
systemd-cgls [PID]
Or to see the full hierarchy:
systemd-cgtop
Several other tools can provide this information:
ps xawf -eo pid,user,cgroup,args
lscgroup -a
cgclassify --list
Here's a bash function to display detailed cgroup information:
function show_cgroups() {
local pid=$1
echo "Cgroup information for PID $pid:"
echo "--------------------------------"
cat /proc/$pid/cgroup
echo
echo "Memory cgroup details:"
cat /sys/fs/cgroup/memory/$(cat /proc/$pid/cgroup | grep memory | cut -d: -f3)/tasks | grep -w $pid
}
A typical output looks like:
12:memory:/user.slice/user-1000.slice/session-1.scope
11:devices:/user.slice
10:net_cls,net_prio:/
9:perf_event:/
8:pids:/user.slice/user-1000.slice/session-1.scope
Each line shows the cgroup hierarchy ID, controller type, and path within the cgroup filesystem.
For containerized processes, you might use:
docker inspect --format '{{.State.Pid}}' container_name | xargs -I {} cat /proc/{}/cgroup