【发布时间】:2018-06-06 03:47:38
【问题描述】:
按照the popular answers to this question 和here 的说明,我在python 3 中创建了以下代码:
p1 = subprocess.Popen(["ps", "-e", "-o", "pcpu,args"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p2 = subprocess.Popen(["cut", "-c", "-132"], stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p3 = subprocess.Popen(["awk", "NR>2"], stdin=p2.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p4 = subprocess.Popen(["sort", "-nr"], stdin=p3.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p5 = subprocess.Popen(["head", "-10"], stdin=p4.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ps,err = p5.communicate()
psout = str(ps, 'utf-8')
此代码在循环中每分钟左右调用一次。与我被引导相信这仍然会产生僵尸相反。 我究竟做错了什么?
编辑:运行代码时的僵尸:
$ ps -eo pid,ppid,state,cmd | awk '$3 == "Z"'
14441 11232 Z [ps] <defunct>
14442 11232 Z [cut] <defunct>
14445 11232 Z [sort] <defunct>
【问题讨论】:
-
也添加相关的
ps输出。 -
嗯...我实际上是指相关的僵尸 (
Z) 进程。这里ps -eo pid,ppid,state,cmd | awk '$3 == "Z"' -
嗯。我的错。更改了最新的编辑以反映这一点。
标签: python subprocess zombie-process