python - 通过 Python 在 Linux 上的进程列表

标签 python linux

如何在 Linux 上使用 Python 获取正在运行的进程列表?

最佳答案

IMO 查看 /proc 文件系统比破解 ps 的文本输出要好。

import os
pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]

for pid in pids:
    try:
        print open(os.path.join('/proc', pid, 'cmdline'), 'rb').read().split('\0')
    except IOError: # proc has already terminated
        continue

关于python - 通过 Python 在 Linux 上的进程列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2703640/

相关文章:

python - Bash - 如何在连接 USB 设备时运行代码

python - django python manage.py runserver RuntimeError : Settings already configured

python - GAE : remote_api and Application Default Credentials

linux - 将输出重定向到命令结果给出的文件名

linux - 如何在 Linux 中正常关机并防止 SDCARD 损坏

python Pandas : Best way to find local maximums in large DF

python - 如果作为 Python 子进程调用,多线程 Perl 脚本会导致管道损坏

linux - vagrant 在 mac 的 debian VM 上设备上没有剩余空间

linux - Bash循环中的计数器增量不起作用

linux - 修改 rootfs (petalinux on zynq)