发布时间:2019-08-12 11:50:48编辑:auto阅读(2501)
#!/usr/bin/env python
import shlex
from subprocess import Popen,PIPE
def get_ps():
cmd = 'ps ax -o pid,ppid,cmd'
p = Popen(shlex.split(cmd),stdout=PIPE)
return p.stdout.readlines()[1:]
def split(s):
s = s.split()
return s[0],s[1],''.join(s[2:])
def parser_ps(data):
procs = []
for l in data:
pid,ppid,cmd = [i.strip() for i in split(l)]
procs.append({'pid':int(pid),'ppid':int(ppid),'cmd':cmd})
return procs
def show(pid,procs,depth=1):
root = [p for p in procs if p['pid'] == pid][0]
print '-' * depth,root['pid'],root['ppid'],root['cmd']
childs = [proc for proc in procs if proc['ppid'] == pid]
if childs:
depth += 1
for c in childs:
show(c['pid'],procs,depth)
if __name__ == '__main__':
data = get_ps()
procs = parser_ps(data)
show(1,procs)
上一篇: Python PK shell ----
下一篇: python链接oracle学习
51283
50735
41333
38144
32611
29513
28364
23233
23199
21526
1597°
2327°
1930°
1870°
2200°
1913°
2600°
4367°
4213°
2991°