Python 官方文档:入门教程 => 点击学习
#!/usr/bin/env python import shlex from subprocess import Popen,PIPE def get_ps(): cmd = 'ps ax -o pid,ppid,cmd' p =
#!/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)
--结束END--
本文标题: python查看进程树
本文链接: https://lsjlt.com/news/185162.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0