后台进程是进程组的成员,而进程组不是 前台进程组 他们的 控制终端 。
相应的字段来自 /proc/PID/stat 是:
/proc/PID/stat
(5) pgrp %d The process group ID of the process. (8) tpgid %d The ID of the foreground process group of the control鈥怽n ling terminal of the process.
因此,这些字段对于后台进程会有所不同。也很有用 (3) state , (7) tty_nr 和 (6) session 。
(3) state
(7) tty_nr
(6) session
(字段编号从1开始)
以下将打印(从启用了作业控制的交互式shell运行时)来自当前会话的所有未停止的后台进程:
awk -vsid=$$ '$6==sid && $3!="T" && $5!=$8 {print $1, $2}' /proc/[0-9]*/stat
为简单起见,假设进程名称(括号中的第二个字段)不包含空格;你必须首先在括号上分割线然后在空间上处理它。
另请注意,这也将打印从子shell开始的进程,这些进程不在shell的作业表中(例如。 (sleep 3600 &) )。
(sleep 3600 &)