Vi.*_*Vi. 18 linux process proc process-groups
在“ /sf/ask/912670041/ ”中,我看到所有答案都提到了ps,没有提到/proc。
“ps”似乎不是很便携(Android 和 Busybox 版本需要不同的参数),我希望能够使用简单且便携的工具列出带有 pgid 的 pid。
在 /proc/.../status 我看到Tgid:(线程组 ID),Gid:(组 ID 用于安全,而不是用于将进程分组在一起),但不是PGid:...
ps从 pid 获取 pgid 的其他(不使用)方法是什么?
cuo*_*glm 25
您可以查看输出中的第 5 个字段/proc/[pid]/stat。
$ ps -ejH | grep firefox
3043 2683 2683 ? 00:00:21 firefox
$ < /proc/3043/stat sed -n '$s/.*) [^ ]* [^ ]* \([^ ]*\).*/\1/p'
2683
Run Code Online (Sandbox Code Playgroud)
来自man proc:
/proc/[pid]/stat
Status information about the process. This is used by ps(1). It is defined in /usr/src/linux/fs/proc/array.c.
The fields, in order, with their proper scanf(3) format specifiers, are:
pid %d The process ID.
comm %s The filename of the executable, in parentheses. This is visible whether or not the executable is swapped out.
state %c One character from the string "RSDZTW" where R is running, S is sleeping in an interruptible wait, D is waiting in
uninterruptible disk sleep, Z is zombie, T is traced or stopped (on a signal), and W is paging.
ppid %d The PID of the parent.
pgrp %d The process group ID of the process.
session %d The session ID of the process.
Run Code Online (Sandbox Code Playgroud)
请注意,您不能使用:
awk '{print $5}'
Run Code Online (Sandbox Code Playgroud)
因为该文件不是空白的分隔列表。第二个字段(进程名称可能包含空格甚至换行符)。例如,大多数线程的firefox名称中通常包含空格字符。
因此,您需要在最后一次出现)字符后打印第三个字段。