/
ps_descendants
executable file
·68 lines (58 loc) · 1.94 KB
/
ps_descendants
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
# ps_descendants -- Lists all descendant PIDs of given PID as well as those sharing the PGID
# $ ps_descendants PID
##
import sys,os,subprocess,re
if len(sys.argv) != 2:
os.execvp("usage", ["usage", sys.argv[0], "Missing PID"])
PID = sys.argv[1]
# list of children PIDs
ps_children = {}
ppid_by_pid = {}
# PIDs by PGID
ps_by_pgid = {}
pgid_by_pid = {}
# take a snapshot of PIDs, PPIDs, PGIDs using ps command, and index them in a handy way
ps_snapshot = subprocess.check_output(["ps", "ax", "-o", "ppid=,pid=,pgid="])
pid_self = str(os.getpid())
for line in ps_snapshot.split("\n"):
line = line.strip()
if len(line) == 0: break
ppid,pid,pgid = re.compile(r" +").split(line)
# avoid indexing this process' children as well as itself
if ppid == pid_self or pid == pid_self: continue
# index PPID edge
if ppid not in ps_children: ps_children[ppid] = set()
ps_children[ppid].add(pid)
ppid_by_pid[pid] = ppid
# group by PGID
if pgid not in ps_by_pgid: ps_by_pgid[pgid] = set()
ps_by_pgid[pgid].add(pid)
pgid_by_pid[pid] = pgid
def descendants(pid):
ds = set([pid])
if pid in ps_children:
for c in ps_children[pid]:
ds |= descendants(c)
return ds
def ancestors(pid):
ppids = []
while pid in ppid_by_pid:
ppid = ppid_by_pid[pid]
ppids.insert(0, ppid)
pid = ppid
return ppids
# list descendants reachable via PID-PPID
pids = descendants(PID)
# as well as those in the same process group except descendants of the highest ancestor other than PID 1
PGID = pgid_by_pid[PID] if PID in pgid_by_pid else PID
if PGID in ps_by_pgid:
ppids = [p for p in ancestors(PID) if p not in ["0", "1"]]
pids |= ps_by_pgid[PGID] - (
descendants(ppids[0]) if len(ppids) > 0 else set()
)
# excluding the given PID as well as this Python process itself
pids -= set([PID])
# output PIDs
if len(pids) > 0:
print "\n".join(pids)