如何在Python中获取进程列表? [英] how do I get the process list in Python?
本文介绍了如何在Python中获取进程列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Unix上从Python获取所有正在运行的进程的进程列表,其中包含命令/进程的名称和进程ID,因此我可以过滤和杀死进程.
How do I get a process list of all running processes from Python, on Unix, containing then name of the command/process and process id, so I can filter and kill processes.
推荐答案
在linux上,最简单的解决方案可能是使用外部ps
命令:
On linux, the easiest solution is probably to use the external ps
command:
>>> import os
>>> data = [(int(p), c) for p, c in [x.rstrip('\n').split(' ', 1) \
... for x in os.popen('ps h -eo pid:1,command')]]
在其他系统上,您可能必须将选项更改为ps
.
On other systems you might have to change the options to ps
.
还是,您可能要在pgrep
和pkill
上运行man
.
Still, you might want to run man
on pgrep
and pkill
.
这篇关于如何在Python中获取进程列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文