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

发布于 2024-07-26 18:31:00 字数 75 浏览 11 评论 0原文

如何在 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

太阳公公是暖光 2024-08-02 18:31:01

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

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

不过,您可能希望在 pgreppkill 上运行 man

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

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

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

晚雾 2024-08-02 18:31:01

安装 psutil:

$pip install psutil

导入 psutil:

>>> import psutil

定义要保存进程列表的列表:

>>> processlist=list()

在列表中附加进程:

>>> for process in psutil.process_iter():
        processlist.append(process.name())

获取进程列表:

>>> print(processlist)

完整代码:

import psutil
processlist=list()
for process in psutil.process_iter():
    processlist.append(process.name())
print(processlist)

Install psutil:

$pip install psutil

Import psutil:

>>> import psutil

Define list where process list is to be saved:

>>> processlist=list()

Append processes in list:

>>> for process in psutil.process_iter():
        processlist.append(process.name())

Get Process list:

>>> print(processlist)

Full code:

import psutil
processlist=list()
for process in psutil.process_iter():
    processlist.append(process.name())
print(processlist)
请帮我爱他 2024-08-02 18:31:01

为什么选择Python?
可以直接使用 killall关于进程名称。

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

不离久伴 2024-08-02 18:31:00

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()

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

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

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

>>> 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()
屌丝范 2024-08-02 18:31:00

在 Linux 上,使用包含 subprocess 模块的最新 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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文