Identify in wich container runs a process?

Hi
I have identified from the host a process by its PID. How to easily determine in which container it is running?

I know “ps fauxww”, or even looping each CT+ps -ef + filtering, but I’d prefer something more efficient.

Same question if I have identified a or several process by their user ID. How to identify to which container this user ID belongs to ?

Happy New Year!

You could try to guess from the cgroup in /proc/PID/cgroup

Brilliant. Thank you!

would be nice to have such a thing directly available in the lxc command like

  • lxc pidinfo list <container>
  • lxc pidinfo getcontainer <pid>

Would it be worth to create a ticket in github?

At the moment I use custom bash functions like

lxc_get_container_pids(){
    CONTAINER=$1
    find /sys/fs/cgroup/lxc.payload.${CONTAINER}/ -name cgroup.procs -exec cat {} \;
}

lxc_get_container_by_pid(){
    PID=$1
    cat /proc/${PID}/cgroup|awk -F '/' '{ split($2,a,".");print a[3] }'
}

Maybe related to systemd-cgtop and lxc-top for one or more lxd hosts #822

2 Likes

I’m curious … why

find /sys/fs/cgroup/lxc.payload.${CONTAINER}/ -name cgroup.procs -exec cat {} \

instead of

cat /sys/fs/cgroup/lxc.payload.${CONTAINER}/cgroup.procs

?

I’m sure there’s a reason. I’m just curious what it is. it’s probably interesting :slight_smile:

You are likely looking for a command like pstree or atop that has knowledge of containers and can automatically group them together.
I would rather see a pstree that has such an option.

pstree has a -S to show namespace transitions. Could be possible to request to show the container name.

           ├─incusd(4568)───systemd(4587,cgroup,ipc,mnt,net,pid,user,uts)─┬─agetty(5049)
           │                                                              ├─cron(5039)
           │                                                              ├─dbus-daemon(5042)
           │                                                              ├─networkd-dispat(5045)
           │                                                              ├─rsyslogd(5046)─┬─{rsyslogd}(5050)
           │                                                              │                └─{rsyslogd}(5051)
           │                                                              ├─systemd-journal(4809)
           │                                                              ├─systemd-logind(5047,mnt,uts)
           │                                                              ├─systemd-network(4889,mnt)
           │                                                              ├─systemd-resolve(4940,mnt)
           │                                                              └─systemd-udevd(4860,mnt,uts)