Determine which group(s) a running process is in?
The list of groups is given under Groups
in /proc/
<pid>/status
; for example,
$ grep '^Groups' /proc/$$/status
Groups: 4 24 27 30 46 110 115 116 1000
The primary group is given under Gid
:
$ grep '^Gid' /proc/$$/status
Gid: 1000 1000 1000 1000
ps
is also capable of showing the groups of a process, as the other answers indicate.
For the effective group id, real group id and supplementary group ids (as used for access control):
ps -o gid,rgid,supgid -p "$pid"
gid
and rgid
are fairly portable, supgid
less so (all 3 would be available with the ps
from procps as typically found on Linux-based systems).
group
, rgroup
and supgrp
can be used to translate group ids to group names, but note that for group ids that have several corresponding group names, only one of them will be shown (same as for ls -l
vs ls -n
or anything that deals with user or group names based on ids).
For the process group id (as used for terminal job control):
ps -o pgid -p "$pid"
To store it into a variable:
pgid=$(($(ps -o pgid= -p "$pid")))
Using ps
:
$ ps -o group,supgrp $$
GROUP SUPGRP
muru adm,cdrom,sudo,dip,www-data,plugdev,lpadmin,mlocate,sambashare,lxd,libvirtd,docker,muru
From man ps
, the output columns used for -o
:
egid EGID effective group ID number of the process as a
decimal integer. (alias gid).
egroup EGROUP effective group ID of the process. This will be
the textual group ID, if it can be obtained and
the field width permits, or a decimal
representation otherwise. (alias group).
gid GID see egid. (alias egid).
group GROUP see egroup. (alias egroup).
supgid SUPGID group ids of supplementary groups, if any. See
getgroups(2).
supgrp SUPGRP group names of supplementary groups, if any. See
getgroups(2).