如何在Python中获取进程列表?

时间:2021-07-15 07:16:30

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.

如何从Unix获取所有正在运行的进程的进程列表,包含命令/进程名称和进程ID,因此我可以过滤和终止进程。

4 个解决方案

#1


On linux, the easiest solution is probably to use the external ps command:

在linux上,最简单的解决方案可能是使用外部ps命令:

>>> 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')]]

On other systems you might have to change the options to ps.

在其他系统上,您可能必须将选项更改为ps。

Still, you might want to run man on pgrep and pkill.

不过,你可能想在pgrep和pkill上运行man。

#2


On Linux, with a suitably recent Python which includes the subprocess module:

在Linux上,使用包含子进程模块的最新Python:

from subprocess import Popen, PIPE

process = Popen(['ps', '-eo' ,'pid,args'], stdout=PIPE, stderr=PIPE)
stdout, notused = process.communicate()
for line in stdout.splitlines():
    pid, cmdline = line.split(' ', 1)
    #Do whatever filtering and processing is needed

You may need to tweak the ps command slightly depending on your exact needs.

您可能需要根据具体需要稍微调整ps命令。

#3


The right portable solution in Python is using psutil. You have different APIs to interact with PIDs:

Python中正确的可移植解决方案是使用psutil。您有不同的API与PID交互:

>>> import psutil
>>> psutil.pids()
[1, 2, 3, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, ..., 32498]
>>> psutil.pid_exists(32498)
True
>>> p = psutil.Process(32498)
>>> p.name()
'python'
>>> p.cmdline()
['python', 'script.py']
>>> p.terminate()
>>> p.wait()

...and if you want to "search and kill":

......如果你想“搜索并杀死”:

for p in psutil.process_iter():
    if 'nginx' in p.name() or 'nginx' in ' '.join(p.cmdline()):
        p.terminate()
        p.wait()

#4


Why Python?
You can directly use killall on the process name.

为何选择Python?您可以直接在进程名称上使用killall。

#1


On linux, the easiest solution is probably to use the external ps command:

在linux上,最简单的解决方案可能是使用外部ps命令:

>>> 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')]]

On other systems you might have to change the options to ps.

在其他系统上,您可能必须将选项更改为ps。

Still, you might want to run man on pgrep and pkill.

不过,你可能想在pgrep和pkill上运行man。

#2


On Linux, with a suitably recent Python which includes the subprocess module:

在Linux上,使用包含子进程模块的最新Python:

from subprocess import Popen, PIPE

process = Popen(['ps', '-eo' ,'pid,args'], stdout=PIPE, stderr=PIPE)
stdout, notused = process.communicate()
for line in stdout.splitlines():
    pid, cmdline = line.split(' ', 1)
    #Do whatever filtering and processing is needed

You may need to tweak the ps command slightly depending on your exact needs.

您可能需要根据具体需要稍微调整ps命令。

#3


The right portable solution in Python is using psutil. You have different APIs to interact with PIDs:

Python中正确的可移植解决方案是使用psutil。您有不同的API与PID交互:

>>> import psutil
>>> psutil.pids()
[1, 2, 3, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, ..., 32498]
>>> psutil.pid_exists(32498)
True
>>> p = psutil.Process(32498)
>>> p.name()
'python'
>>> p.cmdline()
['python', 'script.py']
>>> p.terminate()
>>> p.wait()

...and if you want to "search and kill":

......如果你想“搜索并杀死”:

for p in psutil.process_iter():
    if 'nginx' in p.name() or 'nginx' in ' '.join(p.cmdline()):
        p.terminate()
        p.wait()

#4


Why Python?
You can directly use killall on the process name.

为何选择Python?您可以直接在进程名称上使用killall。