通过Python在Linux上的进程列表 [英] Process list on Linux via Python

查看:177
本文介绍了通过Python在Linux上的进程列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I get running process list using Python on Linux?

推荐答案

IMO看着/proc文件系统并不像破解ps的文本输出那样令人讨厌.

IMO looking at the /proc filesystem is less nasty than hacking the text output of 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在Linux上的进程列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆