17

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.

  • 9
    Try ps -o cgroup <pid> – Matthew Ife Dec 9 '13 at 21:03
  • 5
    cat /proc/<pid>/cgroup – c4f4t0r Dec 9 '13 at 22:30
  • @MIfe, yes, you are right, ty very much. This is what I need – zerospiel Dec 10 '13 at 5:08
  • @c4f4t0r, your method are not so good, it just output to me all available controllers, but not cgroup of a process, but ty you too. – zerospiel Dec 10 '13 at 5:09
  • @MIfe that's true, sorry, other option is grep on tasks file, for example grep <pid> /cgroup/cpuset/app1/tasks – c4f4t0r Dec 11 '13 at 9:27
15

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
  • Last command would match as well data about process whose pid is 13378, or 23378, or 33378, etc. – Patrick Mevzek Jan 15 at 21:55
8

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
  • 4
    use pgrep, not ps | grep | awk – Patrick Jan 10 '18 at 14:58
4

From RHEL7 and up, and on some other distro's, I find this util helpful:

$ systemd-cgtop

Before using it, make sure you have DefaultCPUAccounting=yes in /etc/systemd/system.conf.

I even made some improvements to Egbert's script that is still useful and used Patrick's suggestion to use pgrep:

#!/bin/bash
echo "PID  SLICE   SERVICE"
for THISPID in `pgrep $1`; do
  SLICE=$(cat /proc/$THISPID/cgroup | grep '^1:' | awk -F/ '{ print $2 }')
  SERVICE=$(cat /proc/$THISPID/cgroup | grep '^1:' | awk -F/ '{ print $3 }')
  echo "$THISPID $SLICE $SERVICE"
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.