How to find out cgroup of a particular process?

Is there any method to get cgroup of process?

The only one package that I know (cgroup-bin), just provide some manipulations with cgroups and allow to change cgroup of process/list of processes, but no capabilities to know information about cgroup of a particular process.


Solution 1:

Using systemd (PID 3378 for example):

# systemctl status 3378 | grep CGroup
CGroup: /user.slice/user-1000.slice/session-3.scope

Using proc:

# cat /proc/3378/cgroup 
10:memory:/user.slice/user-1000.slice
9:blkio:/user.slice/user-1000.slice
8:net_cls,net_prio:/
7:cpu,cpuacct:/user.slice/user-1000.slice
6:perf_event:/
5:freezer:/
4:cpuset:/
3:pids:/user.slice/user-1000.slice
2:devices:/user.slice/user-1000.slice
1:name=systemd:/user.slice/user-1000.slice/session-3.scope

Looking into /sys/:

# cd /sys/fs/ && find * -name "*.procs" -exec grep 3378 {} /dev/null \; 2> /dev/null
cgroup/memory/user.slice/user-1000.slice/cgroup.procs:3378
cgroup/blkio/user.slice/user-1000.slice/cgroup.procs:3378
cgroup/net_cls,net_prio/cgroup.procs:3378
cgroup/cpu,cpuacct/user.slice/user-1000.slice/cgroup.procs:3378
cgroup/perf_event/cgroup.procs:3378
cgroup/freezer/cgroup.procs:3378
cgroup/cpuset/cgroup.procs:3378
cgroup/pids/user.slice/user-1000.slice/cgroup.procs:3378
cgroup/devices/user.slice/user-1000.slice/cgroup.procs:3378
cgroup/systemd/user.slice/user-1000.slice/session-3.scope/cgroup.procs:3378

Solution 2:

The quickest way to view cgroup of a process is by process name, using this bash script:

#!/bin/bash
THISPID=`ps -eo pid,comm | grep $1 | awk '{print $1}'`
cat /proc/$THISPID/cgroup